-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make emitter errors more helpful
- Loading branch information
Showing
5 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { assertString, assertFunction, assertNumber } from './assertions'; | ||
|
||
describe('Tests assert functions', () => { | ||
test('successfully validates a string', () => { | ||
const testValue = 'test'; | ||
expect(() => assertString(testValue, 'testValue')).not.toThrow(); | ||
}); | ||
|
||
test('throws error for non-string', () => { | ||
const testValue = 123; | ||
expect(() => assertString(testValue, 'testValue')).toThrow(TypeError); | ||
}); | ||
|
||
test('successfully validates a function', () => { | ||
const testValue = () => {}; // dummy function | ||
expect(() => assertFunction(testValue, 'testValue')).not.toThrow(); | ||
}); | ||
|
||
test('throws error for non-function', () => { | ||
const testValue: any = 123; | ||
expect(() => assertFunction(testValue, 'testValue')).toThrow(TypeError); | ||
}); | ||
|
||
test('successfully validates a number', () => { | ||
const testValue = 123; | ||
expect(() => assertNumber(testValue, 'testValue')).not.toThrow(); | ||
}); | ||
|
||
test('throws error for non-number', () => { | ||
const testValue: any = 'test'; | ||
expect(() => assertNumber(testValue, 'testValue')).toThrow(TypeError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export function assertString( | ||
value: string | number | symbol, | ||
name: string, | ||
): asserts value is string { | ||
assertType('string', value, name); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
export function assertFunction(value: Function, name: string): asserts value is Function { | ||
assertType('function', value, name); | ||
} | ||
|
||
export function assertNumber(value: number, name: string): asserts value is number { | ||
assertType('number', value, name); | ||
} | ||
|
||
function assertType(type: 'string' | 'function' | 'number', value: unknown, name: string) { | ||
if (typeof value !== type) { | ||
throw new TypeError(`${name} must be a ${type}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters