forked from jestjs/jest
/
toThrowMatchers.test.js
547 lines (482 loc) Β· 16.5 KB
/
toThrowMatchers.test.js
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/**
* 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.
*/
'use strict';
const jestExpect = require('../');
// Custom Error class because node versions have different stack trace strings.
class customError extends Error {
constructor(message) {
super();
this.message = message;
this.name = 'Error';
this.stack =
'Error\n' +
' at jestExpect' +
' (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)';
}
}
['toThrowError', 'toThrow'].forEach(toThrow => {
describe(toThrow, () => {
class Err extends customError {}
class Err2 extends customError {}
test('to throw or not to throw', () => {
jestExpect(() => {
throw new customError('apple');
})[toThrow]();
jestExpect(() => {}).not[toThrow]();
});
describe('substring', () => {
it('passes', () => {
jestExpect(() => {
throw new customError('apple');
})[toThrow]('apple');
jestExpect(() => {
throw new customError('banana');
}).not[toThrow]('apple');
jestExpect(() => {}).not[toThrow]('apple');
});
test('did not throw at all', () => {
expect(() =>
jestExpect(() => {})[toThrow]('apple'),
).toThrowErrorMatchingSnapshot();
});
test('threw, but message did not match (error)', () => {
expect(() => {
jestExpect(() => {
throw new customError('apple');
})[toThrow]('banana');
}).toThrowErrorMatchingSnapshot();
});
test('threw, but message did not match (non-error falsey)', () => {
expect(() => {
jestExpect(() => {
// eslint-disable-next-line no-throw-literal
throw '';
})[toThrow]('Server Error');
}).toThrowErrorMatchingSnapshot();
});
it('properly escapes strings when matching against errors', () => {
jestExpect(() => {
throw new TypeError('"this"? throws.');
})[toThrow]('"this"? throws.');
});
test('threw, but message should not match (error)', () => {
expect(() => {
jestExpect(() => {
throw new customError('Invalid array length');
}).not[toThrow]('array');
}).toThrowErrorMatchingSnapshot();
});
test('threw, but message should not match (non-error truthy)', () => {
expect(() => {
jestExpect(() => {
// eslint-disable-next-line no-throw-literal
throw 'Internal Server Error';
}).not[toThrow]('Server Error');
}).toThrowErrorMatchingSnapshot();
});
});
describe('regexp', () => {
it('passes', () => {
expect(() => {
throw new customError('apple');
})[toThrow](/apple/);
expect(() => {
throw new customError('banana');
}).not[toThrow](/apple/);
expect(() => {}).not[toThrow](/apple/);
});
test('did not throw at all', () => {
expect(() =>
jestExpect(() => {})[toThrow](/apple/),
).toThrowErrorMatchingSnapshot();
});
test('threw, but message did not match (error)', () => {
expect(() => {
jestExpect(() => {
throw new customError('apple');
})[toThrow](/banana/);
}).toThrowErrorMatchingSnapshot();
});
test('threw, but message did not match (non-error falsey)', () => {
expect(() => {
jestExpect(() => {
// eslint-disable-next-line no-throw-literal
throw 0;
})[toThrow](/^[123456789]\d*/);
}).toThrowErrorMatchingSnapshot();
});
test('threw, but message should not match (error)', () => {
expect(() => {
jestExpect(() => {
throw new customError('Invalid array length');
}).not[toThrow](/ array /);
}).toThrowErrorMatchingSnapshot();
});
test('threw, but message should not match (non-error truthy)', () => {
expect(() => {
jestExpect(() => {
// eslint-disable-next-line no-throw-literal
throw 404;
}).not[toThrow](/^[123456789]\d*/);
}).toThrowErrorMatchingSnapshot();
});
});
describe('error class', () => {
class SubErr extends Err {
constructor(...args) {
super(...args);
// In a carefully written error subclass,
// name property is equal to constructor name.
this.name = this.constructor.name;
}
}
class SubSubErr extends SubErr {
constructor(...args) {
super(...args);
// In a carefully written error subclass,
// name property is equal to constructor name.
this.name = this.constructor.name;
}
}
it('passes', () => {
jestExpect(() => {
throw new Err();
})[toThrow](Err);
jestExpect(() => {
throw new Err();
})[toThrow](customError);
jestExpect(() => {
throw new Err();
}).not[toThrow](Err2);
jestExpect(() => {}).not[toThrow](Err);
});
test('did not throw at all', () => {
expect(() =>
expect(() => {})[toThrow](Err),
).toThrowErrorMatchingSnapshot();
});
test('threw, but class did not match (error)', () => {
expect(() => {
jestExpect(() => {
throw new Err('apple');
})[toThrow](Err2);
}).toThrowErrorMatchingSnapshot();
});
test('threw, but class did not match (non-error falsey)', () => {
expect(() => {
jestExpect(() => {
// eslint-disable-next-line no-throw-literal
throw undefined;
})[toThrow](Err2);
}).toThrowErrorMatchingSnapshot();
});
test('threw, but class should not match (error)', () => {
expect(() => {
jestExpect(() => {
throw new Err('apple');
}).not[toThrow](Err);
}).toThrowErrorMatchingSnapshot();
});
test('threw, but class should not match (error subclass)', () => {
expect(() => {
jestExpect(() => {
throw new SubErr('apple');
}).not[toThrow](Err);
}).toThrowErrorMatchingSnapshot();
});
test('threw, but class should not match (error subsubclass)', () => {
expect(() => {
jestExpect(() => {
throw new SubSubErr('apple');
}).not[toThrow](Err);
}).toThrowErrorMatchingSnapshot();
});
});
describe('error-message', () => {
// Received message in report if object has message property.
class ErrorMessage {
// not extending Error!
constructor(message) {
this.message = message;
}
}
const expected = new ErrorMessage('apple');
describe('pass', () => {
test('isNot false', () => {
jestExpect(() => {
throw new ErrorMessage('apple');
})[toThrow](expected);
});
test('isNot true', () => {
jestExpect(() => {
throw new ErrorMessage('banana');
}).not[toThrow](expected);
});
});
describe('fail', () => {
test('isNot false', () => {
expect(() =>
jestExpect(() => {
throw new ErrorMessage('banana');
})[toThrow](expected),
).toThrowErrorMatchingSnapshot();
});
test('isNot true', () => {
const message = 'Invalid array length';
expect(() =>
jestExpect(() => {
throw new ErrorMessage(message);
}).not[toThrow]({message}),
).toThrowErrorMatchingSnapshot();
});
test('multiline diff highlight incorrect expected space', () => {
// jest/issues/2673
const a =
"There is no route defined for key Settings. \nMust be one of: 'Home'";
const b =
"There is no route defined for key Settings.\nMust be one of: 'Home'";
expect(() =>
jestExpect(() => {
throw new ErrorMessage(b);
})[toThrow]({message: a}),
).toThrowErrorMatchingSnapshot();
});
});
});
describe('asymmetric', () => {
describe('any-Class', () => {
describe('pass', () => {
test('isNot false', () => {
jestExpect(() => {
throw new Err('apple');
})[toThrow](expect.any(Err));
});
test('isNot true', () => {
jestExpect(() => {
throw new Err('apple');
}).not[toThrow](expect.any(Err2));
});
});
describe('fail', () => {
test('isNot false', () => {
expect(() =>
jestExpect(() => {
throw new Err('apple');
})[toThrow](expect.any(Err2)),
).toThrowErrorMatchingSnapshot();
});
test('isNot true', () => {
expect(() =>
jestExpect(() => {
throw new Err('apple');
}).not[toThrow](expect.any(Err)),
).toThrowErrorMatchingSnapshot();
});
});
});
describe('anything', () => {
describe('pass', () => {
test('isNot false', () => {
jestExpect(() => {
throw new customError('apple');
})[toThrow](expect.anything());
});
test('isNot true', () => {
jestExpect(() => {}).not[toThrow](expect.anything());
jestExpect(() => {
// eslint-disable-next-line no-throw-literal
throw null;
}).not[toThrow](expect.anything());
});
});
describe('fail', () => {
test('isNot false', () => {
expect(() =>
jestExpect(() => {
// eslint-disable-next-line no-throw-literal
throw null;
})[toThrow](expect.anything()),
).toThrowErrorMatchingSnapshot();
});
test('isNot true', () => {
expect(() =>
jestExpect(() => {
throw new customError('apple');
}).not[toThrow](expect.anything()),
).toThrowErrorMatchingSnapshot();
});
});
});
describe('no-symbol', () => {
// Test serialization of asymmetric matcher which has no property:
// this.$$typeof = Symbol.for('jest.asymmetricMatcher')
const matchError = {
asymmetricMatch(received) {
return (
received !== null &&
received !== undefined &&
received.name === 'Error'
);
},
};
const matchNotError = {
asymmetricMatch(received) {
return (
received !== null &&
received !== undefined &&
received.name !== 'Error'
);
},
};
describe('pass', () => {
test('isNot false', () => {
jestExpect(() => {
throw new customError('apple');
})[toThrow](matchError);
});
test('isNot true', () => {
jestExpect(() => {
throw new customError('apple');
}).not[toThrow](matchNotError);
});
});
describe('fail', () => {
test('isNot false', () => {
expect(() =>
jestExpect(() => {
throw new customError('apple');
})[toThrow](matchNotError),
).toThrowErrorMatchingSnapshot();
});
test('isNot true', () => {
expect(() =>
jestExpect(() => {
throw new customError('apple');
}).not[toThrow](matchError),
).toThrowErrorMatchingSnapshot();
});
});
});
describe('objectContaining', () => {
const matchError = expect.objectContaining({
name: 'Error',
});
const matchNotError = expect.objectContaining({
name: 'NotError',
});
describe('pass', () => {
test('isNot false', () => {
jestExpect(() => {
throw new customError('apple');
})[toThrow](matchError);
});
test('isNot true', () => {
jestExpect(() => {
throw new customError('apple');
}).not[toThrow](matchNotError);
});
});
describe('fail', () => {
test('isNot false', () => {
expect(() =>
jestExpect(() => {
throw new customError('apple');
})[toThrow](matchNotError),
).toThrowErrorMatchingSnapshot();
});
test('isNot true', () => {
expect(() =>
jestExpect(() => {
throw new customError('apple');
}).not[toThrow](matchError),
).toThrowErrorMatchingSnapshot();
});
});
});
});
describe('promise/async throws if Error-like object is returned', () => {
const asyncFn = async (shouldThrow?: boolean, resolve?: boolean) => {
let err;
if (shouldThrow) {
err = new Err('async apple');
}
if (resolve) {
return await Promise.resolve(err || 'apple');
} else {
return await Promise.reject(err || 'apple');
}
};
test('passes', async () => {
expect.assertions(24);
await jestExpect(Promise.reject(new Error())).rejects[toThrow]();
await jestExpect(asyncFn(true)).rejects[toThrow]();
await jestExpect(asyncFn(true)).rejects[toThrow](Err);
await jestExpect(asyncFn(true)).rejects[toThrow](Error);
await jestExpect(asyncFn(true)).rejects[toThrow]('apple');
await jestExpect(asyncFn(true)).rejects[toThrow](/app/);
await jestExpect(asyncFn(true)).rejects.not[toThrow](Err2);
await jestExpect(asyncFn(true)).rejects.not[toThrow]('banana');
await jestExpect(asyncFn(true)).rejects.not[toThrow](/banana/);
await jestExpect(asyncFn(true, true)).resolves[toThrow]();
await jestExpect(asyncFn(false, true)).resolves.not[toThrow]();
await jestExpect(asyncFn(false, true)).resolves.not[toThrow](Error);
await jestExpect(asyncFn(false, true)).resolves.not[toThrow]('apple');
await jestExpect(asyncFn(false, true)).resolves.not[toThrow](/apple/);
await jestExpect(asyncFn(false, true)).resolves.not[toThrow]('banana');
await jestExpect(asyncFn(false, true)).resolves.not[toThrow](/banana/);
await jestExpect(asyncFn()).rejects.not[toThrow]();
await jestExpect(asyncFn()).rejects.not[toThrow](Error);
await jestExpect(asyncFn()).rejects.not[toThrow]('apple');
await jestExpect(asyncFn()).rejects.not[toThrow](/apple/);
await jestExpect(asyncFn()).rejects.not[toThrow]('banana');
await jestExpect(asyncFn()).rejects.not[toThrow](/banana/);
// Works with nested functions inside promises
await jestExpect(
Promise.reject(() => {
throw new Error();
}),
).rejects[toThrow]();
await jestExpect(Promise.reject(() => {})).rejects.not[toThrow]();
});
test('did not throw at all', async () => {
await expect(
jestExpect(asyncFn()).rejects[toThrow](),
).rejects.toThrowErrorMatchingSnapshot();
});
test('threw, but class did not match', async () => {
await expect(
jestExpect(asyncFn(true)).rejects[toThrow](Err2),
).rejects.toThrowErrorMatchingSnapshot();
});
test('threw, but should not have', async () => {
await expect(
jestExpect(asyncFn(true)).rejects.not[toThrow](),
).rejects.toThrowErrorMatchingSnapshot();
});
});
describe('expected is undefined', () => {
test('threw, but should not have (non-error falsey)', () => {
expect(() => {
jestExpect(() => {
// eslint-disable-next-line no-throw-literal
throw null;
}).not[toThrow]();
}).toThrowErrorMatchingSnapshot();
});
});
test('invalid arguments', () => {
expect(() =>
jestExpect(() => {}).not[toThrow](111),
).toThrowErrorMatchingSnapshot();
});
test('invalid actual', () => {
expect(() =>
jestExpect('a string')[toThrow](),
).toThrowErrorMatchingSnapshot();
});
});
});