/
invalid-value.ts
107 lines (94 loc) · 3.48 KB
/
invalid-value.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import '../src';
import ansiEscapesSerializer from 'jest-serializer-ansi-escapes';
import * as matcherList from '../src/matchers';
expect.addSnapshotSerializer(ansiEscapesSerializer);
const invalidValueList = [
undefined,
null,
/* boolean */
true,
false,
/* number */
// negative numbers
-0,
-1,
// non finite number
NaN,
Infinity,
-Infinity,
// non integer
0.1,
/* string */
'',
'foo',
'42',
/* bigint */
// negative numbers
BigInt(-1),
/* symbol */
Symbol('vore'),
/* object */
{},
[],
/bar/,
/* function */
() => null,
];
const invalidValuePairList = invalidValueList.map(value => [value, value] as const);
const matcherNameList = Reflect.ownKeys(matcherList)
.filter((key): key is keyof typeof matcherList => (
// @ts-expect-error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Expect'.
typeof expect[key] === 'function'
));
describe.each(matcherNameList)('.%s()', matcherName => {
describe('invalid type actual', () => {
const expectedValue = new Uint8Array();
it.each(invalidValueList)(`expect(actual = %p).${matcherName}(…)`, actual => {
expect(() => {
expect(actual)[matcherName](expectedValue);
}).toThrowErrorMatchingSnapshot();
});
it.each(invalidValueList)(`expect(actual = %p).not.${matcherName}(…)`, actual => {
expect(() => {
expect(actual).not[matcherName](expectedValue);
}).toThrowErrorMatchingSnapshot();
});
});
describe('invalid type expected', () => {
const actualValue = new Uint8Array();
it.each(invalidValueList)(`expect(…).${matcherName}(expected = %p)`, expected => {
expect(() =>
expect(actualValue)[matcherName](
// @ts-expect-error TS2345: Argument of type '{} | null | undefined' is not assignable to parameter of type 'BytesData'.
expected,
)
).toThrowErrorMatchingSnapshot();
});
it.each(invalidValueList)(`expect(…).not.${matcherName}(expected = %p)`, expected => {
expect(() =>
expect(actualValue).not[matcherName](
// @ts-expect-error TS2345: Argument of type '{} | null | undefined' is not assignable to parameter of type 'BytesData'.
expected,
)
).toThrowErrorMatchingSnapshot();
});
});
describe('invalid type actual & expected', () => {
it.each(invalidValuePairList)(`expect(actual = %p).${matcherName}(expected = %p)`, (actual, expected) => {
expect(() =>
expect(actual)[matcherName](
// @ts-expect-error TS2345: Argument of type '{} | null | undefined' is not assignable to parameter of type 'BytesData'.
expected,
)
).toThrowErrorMatchingSnapshot();
});
it.each(invalidValuePairList)(`expect(actual = %p).not.${matcherName}(expected = %p)`, (actual, expected) => {
expect(() =>
expect(actual).not[matcherName](
// @ts-expect-error TS2345: Argument of type '{} | null | undefined' is not assignable to parameter of type 'BytesData'.
expected,
)
).toThrowErrorMatchingSnapshot();
});
});
});