-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
816 lines (806 loc) · 40.2 KB
/
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
'use strict';
delete require.cache[require.resolve('..')];
var rejectedOrNot = require('..');
var assert = require('assert');
var semver = require('semver');
var implementations = [
{
name: 'rejected-or-not',
rejects: rejectedOrNot.rejects,
doesNotReject: rejectedOrNot.doesNotReject
}
];
var node8rejects = semver.satisfies(process.version, '>= 8.13.0 < 9');
if (typeof assert.rejects === 'function' && !node8rejects) {
implementations.push({
name: 'official implementation',
rejects: assert.rejects,
doesNotReject: assert.doesNotReject
});
}
function willReject (value) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
reject(value);
}, 10);
});
}
function willResolve (value) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(value);
}, 10);
});
}
function shouldNotBeFulfilled (args) {
assert(false, 'should not be fulfilled: ' + args);
}
function shouldNotBeRejected (args) {
assert(false, 'should not be rejected: ' + args);
}
implementations.forEach(function (impl) {
var name = impl.name;
var rejects = impl.rejects;
var doesNotReject = impl.doesNotReject;
describe(name, function () {
describe('#rejects(promiseFn, [error], [message])', function () {
describe('promiseFn `<Function> | <Promise>`', function () {
describe('if `promiseFn` is a `<Promise>`, awaits the promise then check that the promise is rejected.', function () {
it('rejects with AssertionError if the `promiseFn` is not rejected.', function () {
return rejects(willResolve('GOOD!')).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.message, 'Missing expected rejection.');
});
});
it('resolves if the `promiseFn` is rejected.', function () {
return rejects(willReject('BOMB!')).then(function () {
assert(true);
}, shouldNotBeRejected);
});
});
describe('if `promiseFn` is a `<Function>`, immediately calls the function and awaits the returned promise to complete. It will then check that the promise is rejected.', function () {
it('rejects with AssertionError if result of `promiseFn` function is NOT rejected.', function () {
return rejects(function () {
return willResolve('GOOD!');
}).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.message, 'Missing expected rejection.');
});
});
it('resolves if result of `promiseFn` function is rejected.', function () {
return rejects(function () {
return willReject('BOMB!');
}).then(function () {
assert(true);
}, shouldNotBeRejected);
});
it('if `promiseFn` is a function and it throws an error synchronously, `rejects()` will return a rejected Promise with that error.', function () {
return rejects(function () {
throw new RangeError('synchronous range error');
}).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof RangeError);
assert(err.message === 'synchronous range error');
});
});
it('if the `promiseFn` function does not return a promise, `rejects()` will return a rejected Promise with `ERR_INVALID_RETURN_VALUE` TypeError.', function () {
return rejects(function () {
return 'not a Promise';
}).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert(err.code === 'ERR_INVALID_RETURN_VALUE');
assert.equal(err.message, 'Expected instance of Promise to be returned from the "promiseFn" function but got instance of String.');
});
});
});
describe('if type of `promiseFn` is other than `<Promise>` or `<Function>`, `rejects()` will return a rejected Promise with `ERR_INVALID_ARG_TYPE` TypeError.', function () {
it('string', function () {
return rejects('not a promise or function').then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert(err.code === 'ERR_INVALID_ARG_TYPE');
if (impl.name === 'official implementation' && semver.satisfies(process.version, '< 12.0.0')) {
assert.equal(err.message, 'The "promiseFn" argument must be one of type Function or Promise. Received type string');
} else {
assert(err.message.startsWith('The "promiseFn" argument must be of type function or an instance of Promise. Received type string'), `actual [${err.message}]`);
}
});
});
it('number', function () {
return rejects(9999).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert(err.code === 'ERR_INVALID_ARG_TYPE');
if (impl.name === 'official implementation' && semver.satisfies(process.version, '< 12.0.0')) {
assert.equal(err.message, 'The "promiseFn" argument must be one of type Function or Promise. Received type number');
} else {
assert(err.message.startsWith('The "promiseFn" argument must be of type function or an instance of Promise. Received type number'), `actual [${err.message}]`);
}
});
});
it('null', function () {
return rejects(null).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert(err.code === 'ERR_INVALID_ARG_TYPE');
if (impl.name === 'official implementation' && semver.satisfies(process.version, '< 12.0.0')) {
assert.equal(err.message, 'The "promiseFn" argument must be one of type Function or Promise. Received type object');
} else {
assert(err.message.startsWith('The "promiseFn" argument must be of type function or an instance of Promise. Received null'), `actual [${err.message}]`);
}
});
});
});
});
describe('error `<RegExp> | <Class> | <Function> | <Object> | <Error>`', function () {
describe('if `error` is a `<RegExp>`, validate rejected actual error message using RegExp. Using a regular expression runs `.toString()` on the actual error object, and will therefore also include the error name.', function () {
it('when message matches, resolves with undefined.', function () {
return rejects(
willReject(new TypeError('Wrong type')),
/^TypeError: Wrong type$/
).then(function (nothing) {
assert(nothing === undefined);
}, shouldNotBeRejected);
});
// improvement of regular expression validation introduced in Node 12.5.0
// see: https://github.com/nodejs/node/commit/ef8f147b7e
if (impl.name === 'official implementation' && semver.satisfies(process.version, '< 12.5.0')) {
it('when message does not match, rejects with the actual error.', function () {
return rejects(
willReject(new TypeError('the original error message')),
/^will not match$/
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert.equal(err.message, 'the original error message');
});
});
} else {
it('when message does not match, rejects with AssertionError.', function () {
return rejects(
willReject(new TypeError('the original error message')),
/^will not match$/
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.message, "The input did not match the regular expression /^will not match$/. Input:\n\n'TypeError: the original error message'\n");
});
});
}
});
describe('if `error` is a `<Class>` (constructor function), validate instanceof using constructor (works well with ES2015 classes that extends Error).', function () {
it('when actual error is an instanceof `<Class>`, resolves with undefined.', function () {
return rejects(
willReject(new TypeError('Wrong type')),
Error
).then(function (nothing) {
assert(nothing === undefined);
}, shouldNotBeRejected);
});
// < Node13
if (impl.name === 'official implementation' && semver.satisfies(process.version, '< 13.0.0')) {
it('when actual error is NOT an instanceof `<Class>`, rejects with the actual error.', function () {
return rejects(
willReject(new TypeError('the original error message')),
RangeError
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert.equal(err.message, 'the original error message');
});
});
} else {
it('when actual error is NOT an instanceof `<Class>`, rejects with AssertionError.', function () {
var te = new TypeError('the original error message');
return rejects(
willReject(te),
RangeError
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.actual, te);
assert.equal(err.expected, RangeError);
assert(/The error is expected to be an instance of "RangeError". Received "TypeError"/.test(err.message));
assert(/the original error message/.test(err.message));
});
});
}
describe('works well with ES2015 classes that extends Error', function () {
class ES2015Error extends Error {
}
class AnotherES2015Error extends Error {
}
it('match case, resolves with undefined.', function () {
return rejects(
willReject(new ES2015Error('foo')),
ES2015Error
).then(function (nothing) {
assert(nothing === undefined);
}, shouldNotBeRejected);
});
// < Node13
if (impl.name === 'official implementation' && semver.satisfies(process.version, '< 13.0.0')) {
it('unmatch case, rejects with the original error.', function () {
return rejects(
willReject(new AnotherES2015Error('bar')),
ES2015Error
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof AnotherES2015Error);
assert.equal(err.message, 'bar');
});
});
} else {
it('unmatch case, rejects with AssertionError.', function () {
var another = new AnotherES2015Error('bar');
return rejects(
willReject(another),
ES2015Error
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.actual, another);
assert.equal(err.expected, ES2015Error);
assert(/The error is expected to be an instance of "ES2015Error". Received "AnotherES2015Error"/.test(err.message));
assert(/bar/.test(err.message));
});
});
}
});
it('appends `error.name` as expected error class name to the message if the `promiseFn` is not rejected.', function () {
return rejects(
willResolve('GOOD!'),
TypeError
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.message, 'Missing expected rejection (TypeError).');
});
});
});
describe('if `error` is a `<Function>`, run custom validation against actual rejection result.', function () {
it('when validation function returns `true`, resolves with undefined.', function () {
return rejects(
willReject(new RangeError('Wrong range')),
function (err) {
return ((err instanceof RangeError) && /range/.test(err));
}
).then(function (nothing) {
assert(nothing === undefined);
}, shouldNotBeRejected);
});
// < Node13
if (impl.name === 'official implementation' && semver.satisfies(process.version, '< 13.0.0')) {
it('when returned value of validation function is NOT `true`, rejects with the actual error.', function () {
return rejects(
willReject(new RangeError('Wrong range')),
function (err) {
return ((err instanceof TypeError) && /type/.test(err));
}
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof RangeError);
assert.equal(err.message, 'Wrong range');
});
});
} else {
it('when returned value of validation function is NOT `true`, rejects with AssertionError.', function () {
var e = new RangeError('Wrong range');
const handlerFn = (err) => {
return ((err instanceof TypeError) && /type/.test(err));
};
return rejects(
willReject(e),
handlerFn
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.actual, e);
assert.equal(err.expected, handlerFn);
assert(/The "handlerFn" validation function is expected to return "true". Received false/.test(err.message), `actual [${err.message}]`);
assert(/RangeError: Wrong range/.test(err.message), `actual [${err.message}]`);
});
});
}
it('if Error is thrown from validation function, rejects with the error.', function () {
var e = new RangeError('the original error message');
var te = new TypeError('some programming error');
return rejects(
willReject(e),
function () {
throw te;
}
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert(err === te);
assert.equal(err.message, 'some programming error');
});
});
});
describe('if `error` is an `<Object>`, that is an object where each property will be tested for.', function () {
it('when all key-value pairs in `error` are the same as key-value pairs from actual rejected result, resolves with undefined. Note that only properties on the error object will be tested.', function () {
var te = new TypeError('Wrong type');
te.code = 'ERR_INVALID_ARG_TYPE';
return rejects(
willReject(te),
{
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE'
}
).then(function (nothing) {
assert(nothing === undefined);
}, shouldNotBeRejected);
});
it('when some of the properties are not same, rejects with AssertionError.', function () {
var te = new TypeError('Wrong type');
te.code = 'ERR_INVALID_ARG_TYPE';
return rejects(
willReject(te),
{
name: 'Error',
message: 'Wrong type',
code: 'ERR_INVALID_RETURN_VALUE'
}
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === te);
});
});
it('when actual result does not have property that `error` have, rejects with AssertionError.', function () {
var te = new TypeError('Wrong type');
return rejects(
willReject(te),
{
name: 'TypeError',
reason: 'Some reason'
}
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === te);
});
});
it('if exists, appends `error.name` as expected error class name to the message if the `promiseFn` is not rejected.', function () {
return rejects(
willResolve('GOOD!'),
{
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE'
}
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.message, 'Missing expected rejection (TypeError).');
});
});
});
describe('if `error` is an `<Error>`, that is an instance of error where each property will be tested for, including the non-enumerable message and name properties.', function () {
it('when all key-value pairs in `error` (error instance in this case) are the same as actual error, resolves with undefined. Note that only properties on the `error` will be tested.', function () {
var te = new TypeError('Wrong type');
te.code = 'ERR_INVALID_ARG_TYPE';
return rejects(
willReject(te),
new TypeError('Wrong type')
).then(function (nothing) {
assert(nothing === undefined);
}, shouldNotBeRejected);
});
it('when some of the properties are not same, rejects with AssertionError.', function () {
var otherErr = new RangeError('Not found');
otherErr.code = 404;
return rejects(
willReject(otherErr),
new TypeError('Wrong type')
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === otherErr);
});
});
it('appends `error.name` as expected error class name to the message if the `promiseFn` is not rejected.', function () {
return rejects(
willResolve('GOOD!'),
new TypeError('Wrong type')
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.message, 'Missing expected rejection (TypeError).');
});
});
});
describe('note that `error` cannot be a string.', function () {
describe('if a string is provided as the second argument,', function () {
it('and the third argument is not given, then `error` is assumed to be omitted and the string will be used for `message` instead. This can lead to easy-to-miss mistakes.', function () {
return rejects(
willResolve('GOOD!'),
'This can lead to easy-to-miss mistakes.'
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.message, 'Missing expected rejection: This can lead to easy-to-miss mistakes.');
});
});
it('and the third argument is also given, reject TypeError with code `ERR_INVALID_ARG_TYPE`.', function () {
return rejects(
willResolve('GOOD!'),
'This can lead to easy-to-miss mistakes.',
'This is clearly a mistake.'
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
if (impl.name === 'official implementation' && semver.satisfies(process.version, '< 12.0.0')) {
assert.equal(err.message, 'The "error" argument must be one of type Object, Error, Function, or RegExp. Received type string');
} else {
assert(err.message.startsWith('The "error" argument must be of type function or an instance of Error, RegExp, or Object. Received type string'), `actual [${err.message}]`);
}
});
});
it('and is identical to the message property of actual error, reject TypeError with code `ERR_AMBIGUOUS_ARGUMENT`.', function () {
return rejects(
willReject(new TypeError('Wrong type')),
'Wrong type'
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert(err.code === 'ERR_AMBIGUOUS_ARGUMENT');
assert.equal(err.message, 'The "error/message" argument is ambiguous. The error message "Wrong type" is identical to the message.');
});
});
it('and is identical to the actual rejected object, reject TypeError with code `ERR_AMBIGUOUS_ARGUMENT`.', function () {
return rejects(
willReject('Rejection Reason'),
'Rejection Reason'
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert(err.code === 'ERR_AMBIGUOUS_ARGUMENT');
assert.equal(err.message, 'The "error/message" argument is ambiguous. The error "Rejection Reason" is identical to the message.');
});
});
});
});
});
describe('message `<any>`', function () {
describe('if specified, `message` will be the message provided by the AssertionError if the `promiseFn` fails to reject.', function () {
it('when `error` is one of `<Class>`, `<Error>` or `<Object>` with `name` property, append it as expected error class name to the assertion message.', function () {
return rejects(
willResolve('GOOD!'),
TypeError,
'MUST BE REJECTED but resolved'
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.message, 'Missing expected rejection (TypeError): MUST BE REJECTED but resolved');
});
});
it('assertion message without expected error class name (when `error` is RegExp or Fuction)', function () {
return rejects(
willResolve('GOOD!'),
/Wrong value/,
'MUST BE REJECTED but resolved'
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.message, 'Missing expected rejection: MUST BE REJECTED but resolved');
});
});
});
describe('`message` argument is also used with `error` of type `<Object>` or `<Error>`', function () {
it('when `error` is an `<Object>` and comparison fails, rejects AssertionError with specified failure message', function () {
var te = new TypeError('Wrong type');
te.code = 'ERR_INVALID_ARG_TYPE';
return rejects(
willReject(te),
{
code: 'ERR_INVALID_RETURN_VALUE'
},
'rejected error must have code ERR_INVALID_RETURN_VALUE'
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === te);
assert.equal(err.message, 'rejected error must have code ERR_INVALID_RETURN_VALUE');
});
});
it('when `error` is an `<Error>` and comparison fails, rejects AssertionError with specified failure message', function () {
var e = new RangeError('Wrong range');
return rejects(
willReject(e),
new TypeError('Wrong type'),
'rejected error must be TypeError with message `Wrong type`'
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === e);
assert.equal(err.message, 'rejected error must be TypeError with message `Wrong type`');
});
});
});
it('`message` argument accepts <any>', function () {
return rejects(
willResolve('GOOD!'),
null,
1234
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.message, 'Missing expected rejection: 1234');
});
});
});
describe('edge cases', function () {
it('when `error` is null, works as if `promiseFn` and `message` are given', function () {
return rejects(
willResolve('GOOD!'),
null,
'MUST BE REJECTED but resolved'
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.message, 'Missing expected rejection: MUST BE REJECTED but resolved');
});
});
it('when `error` is null and `message` is also null, works as if only `promiseFn` is given', function () {
return rejects(
willResolve('GOOD!'),
null,
null
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert.equal(err.message, 'Missing expected rejection.');
});
});
});
});
describe('#doesNotReject(promiseFn, [error], [message])', function () {
describe('promiseFn `<Function> | <Promise>`', function () {
describe('if `promiseFn` is a `<Promise>`, awaits the promise then check that the promise is NOT rejected.', function () {
it('rejects with AssertionError if the `promiseFn` is rejected.', function () {
var te = new TypeError('Wrong type');
return doesNotReject(
willReject(te)
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === te);
assert.equal(err.message, 'Got unwanted rejection.\nActual message: "Wrong type"');
});
});
it('resolves if the `promiseFn` is not rejected.', function () {
return doesNotReject(willResolve('GOOD!')).then(function () {
assert(true);
}, shouldNotBeRejected);
});
});
describe('if `promiseFn` is a `<Function>`, immediately calls the function and awaits the returned promise to complete. It will then check that the promise is NOT rejected.', function () {
it('rejects with AssertionError if the promise returned from `promiseFn` is rejected.', function () {
var te = new TypeError('Wrong type');
return doesNotReject(function () {
return willReject(te);
}).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === te);
assert.equal(err.message, 'Got unwanted rejection.\nActual message: "Wrong type"');
});
});
it('resolves if the promise returned from `promiseFn` is not rejected.', function () {
return doesNotReject(function () {
return willResolve('GOOD!');
}).then(function () {
assert(true);
}, shouldNotBeRejected);
});
it('if `promiseFn` is a function and it throws an error synchronously, `doesNotReject()` will return a rejected Promise with that error.', function () {
return doesNotReject(function () {
throw new RangeError('synchronous range error');
}).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof RangeError);
assert(err.message === 'synchronous range error');
});
});
it('if the function does not return a promise, `doesNotReject()` will return a rejected Promise with an `ERR_INVALID_RETURN_VALUE` TypeError.', function () {
return doesNotReject(function () {
return 'not a Promise';
}).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert(err.code === 'ERR_INVALID_RETURN_VALUE');
assert.equal(err.message, 'Expected instance of Promise to be returned from the "promiseFn" function but got instance of String.');
});
});
});
describe('if type of `promiseFn` is other than `<Promise>` or `<Function>`, `doesNotReject()` will return a rejected Promise with an `ERR_INVALID_ARG_TYPE` TypeError.', function () {
it('string', function () {
return doesNotReject('not a promise or function').then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert(err.code === 'ERR_INVALID_ARG_TYPE');
if (impl.name === 'official implementation' && semver.satisfies(process.version, '< 12.0.0')) {
assert.equal(err.message, 'The "promiseFn" argument must be one of type Function or Promise. Received type string');
} else {
assert(err.message.startsWith('The "promiseFn" argument must be of type function or an instance of Promise. Received type string'), `actual [${err.message}]`);
}
});
});
it('number', function () {
return doesNotReject(9999).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert(err.code === 'ERR_INVALID_ARG_TYPE');
if (impl.name === 'official implementation' && semver.satisfies(process.version, '< 12.0.0')) {
assert.equal(err.message, 'The "promiseFn" argument must be one of type Function or Promise. Received type number');
} else {
assert(err.message.startsWith('The "promiseFn" argument must be of type function or an instance of Promise. Received type number'), `actual [${err.message}]`);
}
});
});
it('null', function () {
return doesNotReject(null).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert(err.code === 'ERR_INVALID_ARG_TYPE');
if (impl.name === 'official implementation' && semver.satisfies(process.version, '< 12.0.0')) {
assert.equal(err.message, 'The "promiseFn" argument must be one of type Function or Promise. Received type object');
} else {
assert(err.message.startsWith('The "promiseFn" argument must be of type function or an instance of Promise. Received null'), `actual [${err.message}]`);
}
});
});
});
});
describe('error `<RegExp> | <Class> | <Function>`', function () {
describe('if `error` is a `<RegExp>`, validate rejected error message using RegExp. Using a regular expression runs `.toString()` on the error object, and will therefore also include the error name.', function () {
it('when message matches, rejects with AssertionError.', function () {
var e = new RangeError('Should not happen');
return doesNotReject(
willReject(e),
/^RangeError: Should not happen$/
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === e);
assert.equal(err.message, 'Got unwanted rejection.\nActual message: "Should not happen"');
});
});
it('when message does not match, rejects with actual error.', function () {
var e = new TypeError('Another Error');
return doesNotReject(
willReject(e),
/^Error: Should not happen$/
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert(err === e);
assert.equal(err.message, 'Another Error');
});
});
});
describe('if `error` is a `<Class>` (constructor function), validate instanceof using constructor (works well with ES2015 classes that extends Error).', function () {
it('when rejected error is an instanceof `<Class>`, rejects with AssertionError.', function () {
var e = new TypeError('Wrong type');
return doesNotReject(
willReject(e),
Error
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === e);
assert.equal(err.message, 'Got unwanted rejection.\nActual message: "Wrong type"');
});
});
it('when rejected error is NOT an instanceof `<Class>`, rejects with the actual error.', function () {
var e = new RangeError('Wrong range');
return doesNotReject(
willReject(e),
TypeError
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof RangeError);
assert.equal(err.message, 'Wrong range');
});
});
describe('works well with ES2015 class that extends Error', function () {
class ES2015Error extends Error {
}
class AnotherES2015Error extends Error {
}
it('match case, rejects with AssertionError.', function () {
var e = new ES2015Error('foo');
return doesNotReject(
willReject(e),
ES2015Error
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === e);
assert.equal(err.message, 'Got unwanted rejection.\nActual message: "foo"');
});
});
it('unmatch case, rejects with the original error.', function () {
return doesNotReject(
willReject(new AnotherES2015Error('bar')),
ES2015Error
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof AnotherES2015Error);
assert.equal(err.message, 'bar');
});
});
});
});
describe('if `error` is a `<Function>`, run custom validation against rejection result.', function () {
it('when validation function returns `true`, rejects with AssertionError.', function () {
var e = new RangeError('Wrong range');
return doesNotReject(
willReject(e),
function (err) {
return ((err instanceof RangeError) && /range/.test(err));
}
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === e);
assert.equal(err.message, 'Got unwanted rejection.\nActual message: "Wrong range"');
});
});
it('when returned value of validation function is NOT `true`, rejects with the actual error.', function () {
var e = new RangeError('Wrong range');
return doesNotReject(
willReject(e),
function (err) {
return ((err instanceof TypeError) && /type/.test(err));
}
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof RangeError);
assert(err === e);
assert.equal(err.message, 'Wrong range');
});
});
it('if Error is thrown from validation function, rejects with the error.', function () {
var e = new RangeError('Wrong range');
var te = new TypeError('some programming error');
return doesNotReject(
willReject(e),
function () {
throw te;
}
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert(err === te);
assert.equal(err.message, 'some programming error');
});
});
it('validation function can be an arrow function.', function () {
var e = new RangeError('Wrong range');
return doesNotReject(
willReject(e),
(err) => ((err instanceof RangeError) && /range/.test(err))
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === e);
assert.equal(err.message, 'Got unwanted rejection.\nActual message: "Wrong range"');
});
});
});
describe('note that `error` cannot be a string.', function () {
describe('if a string is provided as the second argument,', function () {
it('and the third argument is not given, then `error` is assumed to be omitted and the string will be used for `message` instead. This can lead to easy-to-miss mistakes.', function () {
var e = new TypeError('Wrong type');
return doesNotReject(
willReject(e),
'This can lead to easy-to-miss mistakes.'
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === e);
assert.equal(err.message, 'Got unwanted rejection: This can lead to easy-to-miss mistakes.\nActual message: "Wrong type"');
});
});
it('and the third argument is also given, third argument is just ignored.', function () {
var e = new TypeError('Wrong type');
return doesNotReject(
willReject(e),
'This can lead to easy-to-miss mistakes.',
'This is clearly a mistake.'
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === e);
assert.equal(err.message, 'Got unwanted rejection: This can lead to easy-to-miss mistakes.\nActual message: "Wrong type"');
});
});
});
});
describe('if type of `error` is other than `<RegExp>` or `<Function>` (including `<Class>`)', function () {
it('number', function () {
return doesNotReject(
willReject(new RangeError('Wrong range')),
9999
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof TypeError);
assert(err.code === 'ERR_INVALID_ARG_TYPE');
if (impl.name === 'official implementation' && semver.satisfies(process.version, '< 12.0.0')) {
assert.equal(err.message, 'The "expected" argument must be one of type Function or RegExp. Received type number');
} else {
assert(err.message.startsWith('The "expected" argument must be of type function or an instance of RegExp. Received type number'), `actual [${err.message}]`);
}
});
});
});
});
describe('message `<any>`', function () {
it('if an AssertionError is thrown and a value is provided for the message parameter, the value of message will be appended to the AssertionError message.', function () {
var e = new TypeError('Wrong type');
return doesNotReject(
willReject(e),
TypeError,
'should not be thrown'
).then(shouldNotBeFulfilled, function (err) {
assert(err instanceof assert.AssertionError);
assert(err.actual === e);
assert.equal(err.message, 'Got unwanted rejection: should not be thrown\nActual message: "Wrong type"');
});
});
});
});
});
});