File: should-promise-specific.js

package info (click to toggle)
node-chai-as-promised 7.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 244 kB
  • sloc: makefile: 2; sh: 1
file content (639 lines) | stat: -rw-r--r-- 26,010 bytes parent folder | download
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
"use strict";
require("./support/setup.js");
const shouldPass = require("./support/common.js").shouldPass;
const shouldFail = require("./support/common.js").shouldFail;
const expect = require("chai").expect;

describe("Promise-specific extensions:", () => {
    let promise = null;
    const error = new Error("boo");
    error.myProp = ["myProp value"];
    const custom = "No. I am your father.";

    function assertingDoneFactory(done) {
        return result => {
            try {
                expect(result).to.equal(error);
            } catch (assertionError) {
                done(assertionError);
            }

            done();
        };
    }

    describe("when the promise is fulfilled", () => {
        beforeEach(() => {
            promise = Promise.resolve(42);
        });

        describe(".fulfilled", () => {
            shouldPass(() => promise.should.be.fulfilled);
        });

        describe(".fulfilled passes the fulfilled value", () => {
            shouldPass(() => promise.should.be.fulfilled.then(passedValue => {
                passedValue.should.equal(42);
            }));
        });

        describe(".fulfilled allows chaining", () => {
            shouldPass(() => promise.should.be.fulfilled.and.eventually.equal(42));
        });

        describe(".not.fulfilled", () => {
            shouldFail({
                op: () => promise.should.not.be.fulfilled,
                message: "not to be fulfilled but it was fulfilled with 42"
            });
        });

        describe(".rejected", () => {
            shouldFail({
                op: () => promise.should.be.rejected,
                message: "to be rejected but it was fulfilled with 42"
            });
        });

        describe(".not.rejected passes the fulfilled value", () => {
            shouldPass(() => promise.should.not.be.rejected.then(passedValue => {
                passedValue.should.equal(42);
            }));
        });

        // .not inverts all following assertions so the following test is
        // equivalent to promise.should.eventually.not.equal(31)
        describe(".not.rejected allows chaining", () => {
            shouldPass(() => promise.should.not.be.rejected.and.eventually.equal(31));
        });

        describe(".rejectedWith(TypeError)", () => {
            shouldFail({
                op: () => promise.should.be.rejectedWith(TypeError),
                message: "to be rejected with 'TypeError' but it was fulfilled with 42"
            });
        });
        describe(".not.rejectedWith(TypeError) passes the fulfilled value", () => {
            shouldPass(() => promise.should.not.be.rejectedWith(TypeError).then(passedValue => {
                passedValue.should.equal(42);
            }));
        });

        describe(".not.rejectedWith(TypeError) allows chaining", () => {
            shouldPass(() => promise.should.not.be.rejectedWith(TypeError).and.eventually.equal(31));
        });

        describe(".rejectedWith('message substring')", () => {
            shouldFail({
                op: () => promise.should.be.rejectedWith("message substring"),
                message: "to be rejected with an error including 'message substring' but it was fulfilled with " +
                         "42"
            });
        });
        describe(".rejectedWith(/regexp/)", () => {
            shouldFail({
                op: () => promise.should.be.rejectedWith(/regexp/),
                message: "to be rejected with an error matching /regexp/ but it was fulfilled with 42"
            });
        });
        describe(".rejectedWith(TypeError, 'message substring')", () => {
            shouldFail({
                op: () => promise.should.be.rejectedWith(TypeError, "message substring"),
                message: "to be rejected with 'TypeError' but it was fulfilled with 42"
            });
        });
        describe(".rejectedWith(TypeError, /regexp/)", () => {
            shouldFail({
                op: () => promise.should.be.rejectedWith(TypeError, /regexp/),
                message: "to be rejected with 'TypeError' but it was fulfilled with 42"
            });
        });
        describe(".rejectedWith(errorInstance)", () => {
            shouldFail({
                op: () => promise.should.be.rejectedWith(error),
                message: "to be rejected with 'Error: boo' but it was fulfilled with 42"
            });
        });

        describe(".not.rejected", () => {
            shouldPass(() => promise.should.not.be.rejected);
        });
        describe(".not.rejectedWith(TypeError)", () => {
            shouldPass(() => promise.should.not.be.rejectedWith(TypeError));
        });
        describe(".not.rejectedWith('message substring')", () => {
            shouldPass(() => promise.should.not.be.rejectedWith("message substring"));
        });
        describe(".not.rejectedWith(/regexp/)", () => {
            shouldPass(() => promise.should.not.be.rejectedWith(/regexp/));
        });
        describe(".not.rejectedWith(TypeError, 'message substring')", () => {
            shouldPass(() => promise.should.not.be.rejectedWith(TypeError, "message substring"));
        });
        describe(".not.rejectedWith(TypeError, /regexp/)", () => {
            shouldPass(() => promise.should.not.be.rejectedWith(TypeError, /regexp/));
        });
        describe(".not.rejectedWith(errorInstance)", () => {
            shouldPass(() => promise.should.not.be.rejectedWith(error));
        });

        describe(".should.notify(done)", () => {
            it("should pass the test", done => {
                promise.should.notify(done);
            });
        });
    });

    describe("when the promise is rejected", () => {
        beforeEach(() => {
            promise = Promise.reject(error);
        });

        describe(".fulfilled", () => {
            shouldFail({
                op: () => promise.should.be.fulfilled,
                message: "to be fulfilled but it was rejected with 'Error: boo'"
            });
        });

        describe(".not.fulfilled", () => {
            shouldPass(() => promise.should.not.be.fulfilled);
        });

        describe(".not.fulfilled should allow chaining", () => {
            shouldPass(() => promise.should.not.be.fulfilled.and.eventually.have.property("nonexistent"));
        });

        describe(".not.fulfilled should pass the rejection reason", () => {
            shouldPass(() => promise.should.not.be.fulfilled.then(passedError => {
                passedError.should.equal(error);
            }));
        });

        describe(".rejected", () => {
            shouldPass(() => promise.should.be.rejected);
        });

        describe(".not.rejected", () => {
            shouldFail({
                op: () => promise.should.not.be.rejected,
                message: "not to be rejected but it was rejected with 'Error: boo'"
            });
        });
        describe(".rejected should allow chaining", () => {
            shouldPass(() => promise.should.be.rejected.and.eventually.have.property("myProp"));
        });

        describe(".rejected passes the rejection reason", () => {
            shouldPass(() => promise.should.be.rejected.then(passedError => {
                passedError.should.equal(error);
            }));
        });

        describe(".rejectedWith(theError)", () => {
            shouldPass(() => promise.should.be.rejectedWith(error));
        });

        describe(".not.rejectedWith(theError)", () => {
            shouldFail({
                op: () => promise.should.not.be.rejectedWith(error),
                message: "not to be rejected with 'Error: boo'"
            });
        });

        describe(".rejectedWith(theError) should allow chaining", () => {
            shouldPass(() => promise.should.be.rejectedWith(error).and.eventually.have.property("myProp"));
        });

        describe(".rejectedWith(theError) passes the rejection reason", () => {
            shouldPass(() => promise.should.be.rejectedWith(error).then(passedError => {
                passedError.should.equal(error);
            }));
        });

        describe(".rejectedWith(differentError)", () => {
            shouldFail({
                op: () => promise.should.be.rejectedWith(new Error()),
                message: "to be rejected with 'Error' but it was rejected with 'Error: boo'"
            });
        });

        describe(".not.rejectedWith(differentError)", () => {
            shouldPass(() => promise.should.not.be.rejectedWith(new Error()));
        });

        // Chai 3.5.0 never interprets the 2nd paramter to
        // expect(fn).to.throw(a, b) as a custom error message. This is
        // what we are testing here.
        describe(".rejectedWith(differentError, custom)", () => {
            shouldFail({
                op: () => promise.should.be.rejectedWith(new Error(), custom),
                message: "to be rejected with 'Error' but it was rejected with 'Error: boo'",
                notMessage: custom
            });
        });

        describe(".not.rejectedWith(differentError, custom)", () => {
            shouldPass(() => promise.should.not.be.rejectedWith(new Error(), custom));
        });

        describe("with an Error having message 'foo bar'", () => {
            beforeEach(() => {
                promise = Promise.reject(new Error("foo bar"));
            });

            describe(".rejectedWith('foo')", () => {
                shouldPass(() => promise.should.be.rejectedWith("foo"));
            });

            describe(".not.rejectedWith('foo')", () => {
                shouldFail({
                    op: () => promise.should.not.be.rejectedWith("foo"),
                    message: "not to be rejected with an error including 'foo'"
                });
            });

            describe(".rejectedWith(/bar/)", () => {
                shouldPass(() => promise.should.be.rejectedWith(/bar/));
            });

            describe(".not.rejectedWith(/bar/)", () => {
                shouldFail({
                    op: () => promise.should.not.be.rejectedWith(/bar/),
                    message: "not to be rejected with an error matching /bar/"
                });
            });

            describe(".rejectedWith('quux')", () => {
                shouldFail({
                    op: () => promise.should.be.rejectedWith("quux"),
                    message: "to be rejected with an error including 'quux' but got 'foo bar'"
                });
            });

            describe(".not.rejectedWith('quux')", () => {
                shouldPass(() => promise.should.be.not.rejectedWith("quux"));
            });

            describe(".rejectedWith(/quux/)", () => {
                shouldFail({
                    op: () => promise.should.be.rejectedWith(/quux/),
                    message: "to be rejected with an error matching /quux/ but got 'foo bar'"
                });
            });

            describe(".not.rejectedWith(/quux/)", () => {
                shouldPass(() => promise.should.not.be.rejectedWith(/quux/));
            });

            // Chai 3.5.0 never interprets the 2nd paramter to
            // expect(fn).to.throw(a, b) as a custom error
            // message. This is what we are testing here.
            describe(".rejectedWith('foo', custom)", () => {
                shouldPass(() => promise.should.be.rejectedWith("foo", custom));
            });

            describe(".not.rejectedWith('foo', custom)", () => {
                shouldFail({
                    op: () => promise.should.not.be.rejectedWith("foo", custom),
                    message: "not to be rejected with an error including 'foo'",
                    notMessage: custom
                });
            });

            describe(".rejectedWith(/bar/, custom)", () => {
                shouldPass(() => promise.should.be.rejectedWith(/bar/, custom));
            });

            describe(".not.rejectedWith(/bar/, custom)", () => {
                shouldFail({
                    op: () => promise.should.not.be.rejectedWith(/bar/),
                    message: "not to be rejected with an error matching /bar/",
                    notMessage: custom
                });
            });
        });

        describe("with a RangeError", () => {
            beforeEach(() => {
                promise = Promise.reject(new RangeError());
            });

            describe(".rejectedWith(RangeError)", () => {
                shouldPass(() => promise.should.be.rejectedWith(RangeError));
            });

            describe(".not.rejectedWith(RangeError)", () => {
                shouldFail({
                    op: () => promise.should.not.be.rejectedWith(RangeError),
                    message: "not to be rejected with 'RangeError' but it was rejected with 'RangeError'"
                });
            });

            describe(".rejectedWith(TypeError)", () => {
                shouldFail({
                    op: () => promise.should.be.rejectedWith(TypeError),
                    message: "to be rejected with 'TypeError' but it was rejected with 'RangeError'"
                });
            });

            // Case for issue #64.
            describe(".rejectedWith(Array)", () => {
                shouldFail({
                    op: () => promise.should.be.rejectedWith(Array),
                    message: "to be rejected with 'Array' but it was rejected with 'RangeError'"
                });
            });

            describe(".not.rejectedWith(TypeError)", () => {
                shouldPass(() => promise.should.not.be.rejectedWith(TypeError));
            });
        });

        describe("with a RangeError having a message 'foo bar'", () => {
            beforeEach(() => {
                promise = Promise.reject(new RangeError("foo bar"));
            });

            describe(".rejectedWith(RangeError, 'foo')", () => {
                shouldPass(() => promise.should.be.rejectedWith(RangeError, "foo"));
            });

            describe(".not.rejectedWith(RangeError, 'foo')", () => {
                shouldFail({
                    op: () => promise.should.not.be.rejectedWith(RangeError, "foo"),
                    message: "not to be rejected with 'RangeError' but it was rejected with 'RangeError: foo bar'"
                });
            });

            describe(".rejectedWith(RangeError, /bar/)", () => {
                shouldPass(() => promise.should.be.rejectedWith(RangeError, /bar/));
            });

            describe(".not.rejectedWith(RangeError, /bar/)", () => {
                shouldFail({
                    op: () => promise.should.not.be.rejectedWith(RangeError, /bar/),
                    message: "not to be rejected with 'RangeError' but it was rejected with 'RangeError: foo bar'"
                });
            });

            describe(".rejectedWith(RangeError, 'quux')", () => {
                shouldFail({
                    op: () => promise.should.be.rejectedWith(RangeError, "quux"),
                    message: "to be rejected with an error including 'quux' but got 'foo bar'"
                });
            });
            describe(".rejectedWith(RangeError, /quux/)", () => {
                shouldFail({
                    op: () => promise.should.be.rejectedWith(RangeError, /quux/),
                    message: "to be rejected with an error matching /quux/ but got 'foo bar'"
                });
            });

            describe(".rejectedWith(TypeError, 'foo')", () => {
                shouldFail({
                    op: () => promise.should.be.rejectedWith(TypeError, "foo"),
                    message: "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'"
                });
            });
            describe(".rejectedWith(TypeError, /bar/)", () => {
                shouldFail({
                    op: () => promise.should.be.rejectedWith(TypeError, /bar/),
                    message: "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'"
                });
            });

            describe(".rejectedWith(TypeError, 'quux')", () => {
                shouldFail({
                    op: () => promise.should.be.rejectedWith(TypeError, "quux"),
                    message: "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'"
                });
            });
            describe(".rejectedWith(TypeError, /quux/)", () => {
                shouldFail({
                    op: () => promise.should.be.rejectedWith(TypeError, /quux/),
                    message: "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'"
                });
            });

            describe(".not.rejectedWith(RangeError, 'foo')", () => {
                shouldFail({
                    op: () => promise.should.not.be.rejectedWith(RangeError, "foo"),
                    message: "not to be rejected with 'RangeError' but it was rejected with 'RangeError: foo bar'"
                });
            });
            describe(".not.rejectedWith(RangeError, /bar/)", () => {
                shouldFail({
                    op: () => promise.should.not.be.rejectedWith(RangeError, /bar/),
                    message: "not to be rejected with 'RangeError' but it was rejected with 'RangeError: foo bar'"
                });
            });

            describe(".not.rejectedWith(RangeError, 'quux')", () => {
                shouldPass(() => promise.should.not.be.rejectedWith(RangeError, "quux"));
            });
            describe(".not.rejectedWith(RangeError, /quux/)", () => {
                shouldPass(() => promise.should.not.be.rejectedWith(RangeError, /quux/));
            });
            describe(".not.rejectedWith(TypeError, 'foo')", () => {
                shouldPass(() => promise.should.not.be.rejectedWith(TypeError, "foo"));
            });
            describe(".not.rejectedWith(TypeError, /bar/)", () => {
                shouldPass(() => promise.should.not.be.rejectedWith(TypeError, /bar/));
            });
            describe(".not.rejectedWith(TypeError, 'quux')", () => {
                shouldPass(() => promise.should.not.be.rejectedWith(TypeError, "quux"));
            });
            describe(".not.rejectedWith(TypeError, /quux/)", () => {
                shouldPass(() => promise.should.not.be.rejectedWith(TypeError, /quux/));
            });
            describe(".rejectedWith(RangeError, 'foo', custom)", () => {
                shouldPass(() => promise.should.be.rejectedWith(RangeError, "foo", custom));
            });

            describe(".not.rejectedWith(RangeError, 'foo', custom)", () => {
                shouldFail({
                    op: () => promise.should.not.be.rejectedWith(RangeError, "foo", custom),
                    message: custom
                });
            });

            describe(".rejectedWith(RangeError, /bar/, custom)", () => {
                shouldPass(() => promise.should.be.rejectedWith(RangeError, /bar/, custom));
            });

            describe(".not.rejectedWith(RangeError, /bar/, custom)", () => {
                shouldFail({
                    op: () => promise.should.not.be.rejectedWith(RangeError, /bar/, custom),
                    message: custom
                });
            });

            describe(".rejectedWith(RangeError, 'quux', custom)", () => {
                shouldFail({
                    op: () => promise.should.be.rejectedWith(RangeError, "quux", custom),
                    message: custom
                });
            });

            describe(".not.rejectedWith(TypeError, 'quux', custom)", () => {
                shouldPass(() => promise.should.not.be.rejectedWith(TypeError, "quux", custom));
            });

            describe(".rejectedWith(RangeError, /quux/, custom)", () => {
                shouldFail({
                    op: () => promise.should.be.rejectedWith(RangeError, /quux/, custom),
                    message: custom
                });
            });

            describe(".not.rejectedWith(TypeError, /quux/, custom)", () => {
                shouldPass(() => promise.should.not.be.rejectedWith(TypeError, /quux/, custom));
            });

            describe(".rejectedWith(RangeError, undefined, custom)", () => {
                shouldPass(() => promise.should.be.rejectedWith(RangeError, undefined, custom));
            });

            describe(".not.rejectedWith(RangeError, undefined, custom)", () => {
                shouldFail({
                    op: () => promise.should.not.be.rejectedWith(RangeError, undefined, custom),
                    message: custom
                });
            });

            describe(".rejectedWith(TypeError, undefined, custom)", () => {
                shouldFail({
                    op: () => promise.should.be.rejectedWith(TypeError, undefined, custom),
                    message: custom
                });
            });

            describe(".not.rejectedWith(TypeError, undefined, custom)", () => {
                shouldPass(() => promise.should.not.be.rejectedWith(TypeError, undefined, custom));
            });
        });

        describe(".should.notify(done)", () => {
            it("should fail the test with the original error", done => {
                promise.should.notify(assertingDoneFactory(done));
            });
        });
    });

    describe(".should.notify with chaining (GH-3)", () => {
        describe("the original promise is fulfilled", () => {
            beforeEach(() => {
                promise = Promise.resolve();
            });

            describe("and the follow-up promise is fulfilled", () => {
                beforeEach(() => {
                    promise = promise.then(() => { /* Do nothing */ });
                });

                it("should pass the test", done => {
                    promise.should.notify(done);
                });
            });

            describe("but the follow-up promise is rejected", () => {
                beforeEach(() => {
                    promise = promise.then(() => {
                        throw error;
                    });
                });

                it("should fail the test with the error from the follow-up promise", done => {
                    promise.should.notify(assertingDoneFactory(done));
                });
            });
        });

        describe("the original promise is rejected", () => {
            beforeEach(() => {
                promise = Promise.reject(error);
            });

            describe("but the follow-up promise is fulfilled", () => {
                beforeEach(() => {
                    promise = promise.then(() => { /* Do nothing */ });
                });

                it("should fail the test with the error from the original promise", done => {
                    promise.should.notify(assertingDoneFactory(done));
                });
            });

            describe("and the follow-up promise is rejected", () => {
                beforeEach(() => {
                    promise = promise.then(() => {
                        throw new Error("follow up");
                    });
                });

                it("should fail the test with the error from the original promise", done => {
                    promise.should.notify(assertingDoneFactory(done));
                });
            });
        });
    });

    describe("Using with non-thenables", () => {
        describe("A number", () => {
            const number = 5;

            it("should fail for .fulfilled", () => {
                expect(() => number.should.be.fulfilled).to.throw(TypeError, "not a thenable");
            });
            it("should fail for .rejected", () => {
                expect(() => number.should.be.rejected).to.throw(TypeError, "not a thenable");
            });
            it("should fail for .become", () => {
                expect(() => number.should.become(5)).to.throw(TypeError, "not a thenable");
            });
            it("should fail for .eventually", () => {
                expect(() => number.should.eventually.equal(5)).to.throw(TypeError, "not a thenable");
            });
            it("should fail for .notify", () => {
                expect(() => number.should.notify(() => { /* Doesn't matter */ }))
                    .to.throw(TypeError, "not a thenable");
            });
        });
    });

    describe("Using together with other Chai as Promised asserters", () => {
        describe(".fulfilled.and.eventually.equal(42)", () => {
            shouldPass(() => Promise.resolve(42).should.be.fulfilled.and.eventually.equal(42));
        });
        describe(".fulfilled.and.rejected", () => {
            shouldFail({
                op: () => Promise.resolve(42).should.be.fulfilled.and.rejected,
                message: "to be rejected but it was fulfilled with 42"
            });
        });

        describe(".rejected.and.eventually.equal(42)", () => {
            shouldPass(() => Promise.reject(42).should.be.rejected.and.eventually.equal(42));
        });
        describe(".rejected.and.become(42)", () => {
            shouldPass(() => Promise.reject(42).should.be.rejected.and.become(42));
        });
    });

    describe("With promises that only become rejected later (GH-24)", () => {
        it("should wait for them", done => {
            let reject;
            const rejectedLaterPromise = new Promise((_, r) => {
                reject = r;
            });
            rejectedLaterPromise.should.be.rejectedWith("error message").and.notify(done);

            setTimeout(() => reject(new Error("error message")), 100);
        });
    });

    describe("`rejectedWith` with non-`Error` rejection reasons (GH-33)", () => {
        shouldPass(() => Promise.reject(42).should.be.rejectedWith(42));
    });
});