forked from remeda/remeda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isError.test.ts
31 lines (28 loc) · 927 Bytes
/
isError.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { isError } from './isError';
import { typesDataProvider } from '../test/types_data_provider';
describe('isError', () => {
test('isError: should work as type guard', () => {
const data = typesDataProvider('error');
if (isError(data)) {
expect(data instanceof Error).toEqual(true);
assertType<Error>(data);
}
class MyError extends Error {}
let maybeError: MyError | undefined;
if (isError(maybeError)) {
assertType<MyError>(maybeError);
}
});
test('isError: should work as type guard in filter', () => {
const data = [
typesDataProvider('error'),
typesDataProvider('array'),
typesDataProvider('boolean'),
typesDataProvider('function'),
typesDataProvider('object'),
typesDataProvider('number'),
].filter(isError);
expect(data.every(c => c instanceof Error)).toEqual(true);
assertType<Array<Error>>(data);
});
});