-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
asymmetricMatchers.test.ts
483 lines (430 loc) · 14.6 KB
/
asymmetricMatchers.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import jestExpect from '../';
import {
any,
anything,
arrayContaining,
arrayNotContaining,
closeTo,
notCloseTo,
objectContaining,
objectNotContaining,
stringContaining,
stringMatching,
stringNotContaining,
stringNotMatching,
} from '../asymmetricMatchers';
test('Any.asymmetricMatch()', () => {
class Thing {}
[
any(String).asymmetricMatch('jest'),
any(Number).asymmetricMatch(1),
any(Function).asymmetricMatch(() => {}),
any(Boolean).asymmetricMatch(true),
any(BigInt).asymmetricMatch(1n),
any(Symbol).asymmetricMatch(Symbol()),
any(Object).asymmetricMatch({}),
any(Object).asymmetricMatch(null),
any(Array).asymmetricMatch([]),
any(Thing).asymmetricMatch(new Thing()),
].forEach(test => {
jestExpect(test).toBe(true);
});
});
test('Any.asymmetricMatch() on primitive wrapper classes', () => {
[
// eslint-disable-next-line no-new-wrappers
any(String).asymmetricMatch(new String('jest')),
// eslint-disable-next-line no-new-wrappers
any(Number).asymmetricMatch(new Number(1)),
// eslint-disable-next-line no-new-func
any(Function).asymmetricMatch(new Function('() => {}')),
// eslint-disable-next-line no-new-wrappers
any(Boolean).asymmetricMatch(new Boolean(true)),
any(BigInt).asymmetricMatch(Object(1n)),
any(Symbol).asymmetricMatch(Object(Symbol())),
].forEach(test => {
jestExpect(test).toBe(true);
});
});
test('Any.toAsymmetricMatcher()', () => {
jestExpect(any(Number).toAsymmetricMatcher()).toBe('Any<Number>');
});
test('Any.toAsymmetricMatcher() with function name', () => {
[
['someFunc', function someFunc() {}],
['$someFunc', function $someFunc() {}],
[
'$someFunc2',
(function () {
function $someFunc2() {}
Object.defineProperty($someFunc2, 'name', {value: ''});
return $someFunc2;
})(),
],
[
'$someAsyncFunc',
(function () {
async function $someAsyncFunc() {}
Object.defineProperty($someAsyncFunc, 'name', {value: ''});
return $someAsyncFunc;
})(),
],
[
'$someGeneratorFunc',
(function () {
function* $someGeneratorFunc() {}
Object.defineProperty($someGeneratorFunc, 'name', {value: ''});
return $someGeneratorFunc;
})(),
],
[
'$someFuncWithFakeToString',
(function () {
function $someFuncWithFakeToString() {}
$someFuncWithFakeToString.toString = () => 'Fake to string';
return $someFuncWithFakeToString;
})(),
],
].forEach(([name, fn]) => {
jestExpect(any(fn).toAsymmetricMatcher()).toBe(`Any<${name}>`);
});
});
test('Any throws when called with empty constructor', () => {
jestExpect(() => any()).toThrow();
});
test('Anything matches any type', () => {
[
anything().asymmetricMatch('jest'),
anything().asymmetricMatch(1),
anything().asymmetricMatch(() => {}),
anything().asymmetricMatch(true),
anything().asymmetricMatch({}),
anything().asymmetricMatch([]),
].forEach(test => {
jestExpect(test).toBe(true);
});
});
test('Anything does not match null and undefined', () => {
[
anything().asymmetricMatch(null),
anything().asymmetricMatch(undefined),
].forEach(test => {
jestExpect(test).toBe(false);
});
});
test('Anything.toAsymmetricMatcher()', () => {
jestExpect(anything().toAsymmetricMatcher()).toBe('Anything');
});
test('ArrayContaining matches', () => {
[
arrayContaining([]).asymmetricMatch('jest'),
arrayContaining(['foo']).asymmetricMatch(['foo']),
arrayContaining(['foo']).asymmetricMatch(['foo', 'bar']),
arrayContaining([]).asymmetricMatch({}),
].forEach(test => {
jestExpect(test).toEqual(true);
});
});
test('ArrayContaining does not match', () => {
jestExpect(arrayContaining(['foo']).asymmetricMatch(['bar'])).toBe(false);
});
test('ArrayContaining throws for non-arrays', () => {
jestExpect(() => {
arrayContaining('foo').asymmetricMatch([]);
}).toThrow();
});
test('ArrayNotContaining matches', () => {
jestExpect(arrayNotContaining(['foo']).asymmetricMatch(['bar'])).toBe(true);
});
test('ArrayNotContaining does not match', () => {
[
arrayNotContaining([]).asymmetricMatch('jest'),
arrayNotContaining(['foo']).asymmetricMatch(['foo']),
arrayNotContaining(['foo']).asymmetricMatch(['foo', 'bar']),
arrayNotContaining([]).asymmetricMatch({}),
].forEach(test => {
jestExpect(test).toEqual(false);
});
});
test('ArrayNotContaining throws for non-arrays', () => {
jestExpect(() => {
arrayNotContaining('foo').asymmetricMatch([]);
}).toThrow();
});
test('ObjectContaining matches', () => {
[
objectContaining({}).asymmetricMatch('jest'),
objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foo', jest: 'jest'}),
objectContaining({foo: undefined}).asymmetricMatch({foo: undefined}),
objectContaining({first: objectContaining({second: {}})}).asymmetricMatch({
first: {second: {}},
}),
objectContaining({foo: Buffer.from('foo')}).asymmetricMatch({
foo: Buffer.from('foo'),
jest: 'jest',
}),
].forEach(test => {
jestExpect(test).toEqual(true);
});
});
test('ObjectContaining does not match', () => {
[
objectContaining({foo: 'foo'}).asymmetricMatch({bar: 'bar'}),
objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foox'}),
objectContaining({foo: undefined}).asymmetricMatch({}),
objectContaining({
answer: 42,
foo: {bar: 'baz', foobar: 'qux'},
}).asymmetricMatch({foo: {bar: 'baz'}}),
].forEach(test => {
jestExpect(test).toEqual(false);
});
});
test('ObjectContaining matches defined properties', () => {
const definedPropertyObject = {};
Object.defineProperty(definedPropertyObject, 'foo', {get: () => 'bar'});
jestExpect(
objectContaining({foo: 'bar'}).asymmetricMatch(definedPropertyObject),
).toBe(true);
});
test('ObjectContaining matches prototype properties', () => {
const prototypeObject = {foo: 'bar'};
let obj;
if (Object.create) {
obj = Object.create(prototypeObject);
} else {
function Foo() {}
Foo.prototype = prototypeObject;
Foo.prototype.constructor = Foo;
obj = new Foo();
}
jestExpect(objectContaining({foo: 'bar'}).asymmetricMatch(obj)).toBe(true);
});
test('ObjectContaining throws for non-objects', () => {
jestExpect(() => objectContaining(1337).asymmetricMatch()).toThrow();
});
test('ObjectContaining does not mutate the sample', () => {
const sample = {foo: {bar: {}}};
const sample_json = JSON.stringify(sample);
expect({foo: {bar: {}}}).toEqual(expect.objectContaining(sample));
expect(JSON.stringify(sample)).toEqual(sample_json);
});
test('ObjectNotContaining matches', () => {
[
objectNotContaining({foo: 'foo'}).asymmetricMatch({bar: 'bar'}),
objectNotContaining({foo: 'foo'}).asymmetricMatch({foo: 'foox'}),
objectNotContaining({foo: undefined}).asymmetricMatch({}),
objectNotContaining({
first: objectNotContaining({second: {}}),
}).asymmetricMatch({first: {second: {}}}),
objectNotContaining({first: {second: {}, third: {}}}).asymmetricMatch({
first: {second: {}},
}),
objectNotContaining({first: {second: {}}}).asymmetricMatch({
first: {second: {}, third: {}},
}),
objectNotContaining({foo: 'foo', jest: 'jest'}).asymmetricMatch({
foo: 'foo',
}),
].forEach(test => {
jestExpect(test).toEqual(true);
});
});
test('ObjectNotContaining does not match', () => {
[
objectNotContaining({}).asymmetricMatch('jest'),
objectNotContaining({foo: 'foo'}).asymmetricMatch({
foo: 'foo',
jest: 'jest',
}),
objectNotContaining({foo: undefined}).asymmetricMatch({foo: undefined}),
objectNotContaining({first: {second: {}}}).asymmetricMatch({
first: {second: {}},
}),
objectNotContaining({
first: objectContaining({second: {}}),
}).asymmetricMatch({first: {second: {}}}),
objectNotContaining({}).asymmetricMatch(null),
objectNotContaining({}).asymmetricMatch({}),
].forEach(test => {
jestExpect(test).toEqual(false);
});
});
test('ObjectNotContaining inverts ObjectContaining', () => {
[
[{}, null],
[{foo: 'foo'}, {foo: 'foo', jest: 'jest'}],
[{foo: 'foo', jest: 'jest'}, {foo: 'foo'}],
[{foo: undefined}, {foo: undefined}],
[{foo: undefined}, {}],
[{first: {second: {}}}, {first: {second: {}}}],
[{first: objectContaining({second: {}})}, {first: {second: {}}}],
[{first: objectNotContaining({second: {}})}, {first: {second: {}}}],
[{}, {foo: undefined}],
].forEach(([sample, received]) => {
jestExpect(objectNotContaining(sample).asymmetricMatch(received)).toEqual(
!objectContaining(sample).asymmetricMatch(received),
);
});
});
test('ObjectNotContaining throws for non-objects', () => {
jestExpect(() => objectNotContaining(1337).asymmetricMatch()).toThrow();
});
test('StringContaining matches string against string', () => {
jestExpect(stringContaining('en*').asymmetricMatch('queen*')).toBe(true);
jestExpect(stringContaining('en').asymmetricMatch('queue')).toBe(false);
});
test('StringContaining throws if expected value is not string', () => {
jestExpect(() => {
stringContaining([1]).asymmetricMatch('queen');
}).toThrow();
});
test('StringContaining returns false if received value is not string', () => {
jestExpect(stringContaining('en*').asymmetricMatch(1)).toBe(false);
});
test('StringNotContaining matches string against string', () => {
jestExpect(stringNotContaining('en*').asymmetricMatch('queen*')).toBe(false);
jestExpect(stringNotContaining('en').asymmetricMatch('queue')).toBe(true);
});
test('StringNotContaining throws if expected value is not string', () => {
jestExpect(() => {
stringNotContaining([1]).asymmetricMatch('queen');
}).toThrow();
});
test('StringNotContaining returns true if received value is not string', () => {
jestExpect(stringNotContaining('en*').asymmetricMatch(1)).toBe(true);
});
test('StringMatching matches string against regexp', () => {
jestExpect(stringMatching(/en/).asymmetricMatch('queen')).toBe(true);
jestExpect(stringMatching(/en/).asymmetricMatch('queue')).toBe(false);
});
test('StringMatching matches string against string', () => {
jestExpect(stringMatching('en').asymmetricMatch('queen')).toBe(true);
jestExpect(stringMatching('en').asymmetricMatch('queue')).toBe(false);
});
test('StringMatching throws if expected value is neither string nor regexp', () => {
jestExpect(() => {
stringMatching([1]).asymmetricMatch('queen');
}).toThrow();
});
test('StringMatching returns false if received value is not string', () => {
jestExpect(stringMatching('en').asymmetricMatch(1)).toBe(false);
});
test('StringMatching returns false even if coerced non-string received value matches pattern', () => {
jestExpect(stringMatching('null').asymmetricMatch(null)).toBe(false);
});
test('StringNotMatching matches string against regexp', () => {
jestExpect(stringNotMatching(/en/).asymmetricMatch('queen')).toBe(false);
jestExpect(stringNotMatching(/en/).asymmetricMatch('queue')).toBe(true);
});
test('StringNotMatching matches string against string', () => {
jestExpect(stringNotMatching('en').asymmetricMatch('queen')).toBe(false);
jestExpect(stringNotMatching('en').asymmetricMatch('queue')).toBe(true);
});
test('StringNotMatching throws if expected value is neither string nor regexp', () => {
jestExpect(() => {
stringNotMatching([1]).asymmetricMatch('queen');
}).toThrow();
});
test('StringNotMatching returns true if received value is not string', () => {
jestExpect(stringNotMatching('en').asymmetricMatch(1)).toBe(true);
});
describe('closeTo', () => {
[
[0, 0],
[0, 0.001],
[1.23, 1.229],
[1.23, 1.226],
[1.23, 1.225],
[1.23, 1.234],
[Infinity, Infinity],
[-Infinity, -Infinity],
].forEach(([expected, received]) => {
test(`${expected} closeTo ${received} return true`, () => {
jestExpect(closeTo(expected).asymmetricMatch(received)).toBe(true);
});
test(`${expected} notCloseTo ${received} return false`, () => {
jestExpect(notCloseTo(expected).asymmetricMatch(received)).toBe(false);
});
});
[
[0, 0.01],
[1, 1.23],
[1.23, 1.2249999],
[Infinity, -Infinity],
[Infinity, 1.23],
[-Infinity, -1.23],
].forEach(([expected, received]) => {
test(`${expected} closeTo ${received} return false`, () => {
jestExpect(closeTo(expected).asymmetricMatch(received)).toBe(false);
});
test(`${expected} notCloseTo ${received} return true`, () => {
jestExpect(notCloseTo(expected).asymmetricMatch(received)).toBe(true);
});
});
[
[0, 0.1, 0],
[0, 0.0001, 3],
[0, 0.000004, 5],
[2.0000002, 2, 5],
].forEach(([expected, received, precision]) => {
test(`${expected} closeTo ${received} with precision ${precision} return true`, () => {
jestExpect(closeTo(expected, precision).asymmetricMatch(received)).toBe(
true,
);
});
test(`${expected} notCloseTo ${received} with precision ${precision} return false`, () => {
jestExpect(
notCloseTo(expected, precision).asymmetricMatch(received),
).toBe(false);
});
});
[
[3.141592e-7, 3e-7, 8],
[56789, 51234, -4],
].forEach(([expected, received, precision]) => {
test(`${expected} closeTo ${received} with precision ${precision} return false`, () => {
jestExpect(closeTo(expected, precision).asymmetricMatch(received)).toBe(
false,
);
});
test(`${expected} notCloseTo ${received} with precision ${precision} return true`, () => {
jestExpect(
notCloseTo(expected, precision).asymmetricMatch(received),
).toBe(true);
});
});
test('closeTo throw if expected is not number', () => {
jestExpect(() => {
closeTo('a');
}).toThrow();
});
test('notCloseTo throw if expected is not number', () => {
jestExpect(() => {
notCloseTo('a');
}).toThrow();
});
test('closeTo throw if precision is not number', () => {
jestExpect(() => {
closeTo(1, 'a');
}).toThrow();
});
test('notCloseTo throw if precision is not number', () => {
jestExpect(() => {
notCloseTo(1, 'a');
}).toThrow();
});
test('closeTo return false if received is not number', () => {
jestExpect(closeTo(1).asymmetricMatch('a')).toBe(false);
});
test('notCloseTo return false if received is not number', () => {
jestExpect(notCloseTo(1).asymmetricMatch('a')).toBe(false);
});
});