File: assert-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 (288 lines) | stat: -rw-r--r-- 10,892 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
"use strict";
require("./support/setup.js");
const shouldPass = require("./support/common.js").shouldPass;
const shouldFail = require("./support/common.js").shouldFail;
const assert = require("chai").assert;

describe("Assert interface:", () => {
    let promise = null;
    const error = new Error("boo");
    const custom = "No. I am your father.";

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

        describe(".isFulfilled(promise)", () => {
            shouldPass(() => assert.isFulfilled(promise));
        });

        describe(".becomes(promise, correctValue)", () => {
            shouldPass(() => assert.becomes(promise, { foo: "bar" }));
        });
        describe(".becomes(promise, incorrectValue)", () => {
            shouldFail({
                op: () => assert.becomes(promise, { baz: "quux" }),
                message: "to deeply equal { baz: 'quux' }"
            });
        });

        describe(".becomes(promise, incorrectValue, custom)", () => {
            shouldFail({
                op: () => assert.becomes(promise, { baz: "quux" }, custom),
                message: custom
            });
        });

        describe(".doesNotBecome(promise, correctValue)", () => {
            shouldFail({
                op: () => assert.doesNotBecome(promise, { foo: "bar" }),
                message: "to not deeply equal { foo: 'bar' }"
            });
        });
        describe(".doesNotBecome(promise, incorrectValue)", () => {
            shouldPass(() => assert.doesNotBecome(promise, { baz: "quux" }));
        });

        describe(".doesNotBecome(promise, correctValue, custom)", () => {
            shouldFail({
                op: () => assert.doesNotBecome(promise, { foo: "bar" }, custom),
                message: custom
            });
        });

        describe(".isRejected(promise)", () => {
            shouldFail({
                op: () => assert.isRejected(promise),
                message: "to be rejected"
            });
        });
        describe(".isRejected(promise, TypeError)", () => {
            shouldFail({
                op: () => assert.isRejected(promise, TypeError),
                message: "to be rejected"
            });
        });
        describe(".isRejected(promise, /regexp/)", () => {
            shouldFail({
                op: () => assert.isRejected(promise, /regexp/),
                message: "to be rejected"
            });
        });
        describe(".isRejected(promise, TypeError, /regexp/)", () => {
            shouldFail({
                op: () => assert.isRejected(promise, TypeError, /regexp/),
                message: "to be rejected"
            });
        });
        describe(".isRejected(promise, errorInstance)", () => {
            shouldFail({
                op: () => assert.isRejected(promise, error),
                message: "to be rejected"
            });
        });
        // Chai never interprets the 3rd parameter to assert.throws as
        // a custom error message. This is what we are checking here.
        describe(".isRejected(promise, /quux/, custom)", () => {
            shouldFail({
                op: () => assert.isRejected(promise, /quux/, custom),
                notMessage: custom
            });
        });
    });


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

        describe(".isFulfilled(promise)", () => {
            shouldFail({
                op: () => assert.isFulfilled(promise),
                message: "to be fulfilled"
            });
        });

        describe(".isFulfilled(promise, custom)", () => {
            shouldFail({
                op: () => assert.isFulfilled(promise, custom),
                message: custom
            });
        });

        describe(".isRejected(promise)", () => {
            shouldPass(() => assert.isRejected(promise));
        });

        describe(".isRejected(promise, theError)", () => {
            shouldPass(() => assert.isRejected(promise, error));
        });

        describe(".isRejected(promise, differentError)", () => {
            shouldFail({
                op: () => assert.isRejected(promise, new Error()),
                message: "to be rejected with"
            });
        });

        // Chai never interprets the 3rd parameter to assert.throws as
        // a custom error message. This is what we are checking here.
        describe(".isRejected(promise, differentError, custom)", () => {
            shouldFail({
                op: () => assert.isRejected(promise, new Error(), custom),
                notMessage: custom
            });
        });

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

            describe(".isRejected(promise, 'bar')", () => {
                shouldPass(() => assert.isRejected(promise, "bar"));
            });

            describe(".isRejected(promise, 'bar', custom)", () => {
                shouldPass(() => assert.isRejected(promise, "bar", custom));
            });

            describe(".isRejected(promise, /bar/)", () => {
                shouldPass(() => assert.isRejected(promise, /bar/));
            });

            describe(".isRejected(promise, /bar/, custom)", () => {
                shouldPass(() => assert.isRejected(promise, /bar/, custom));
            });

            describe(".isRejected(promise, 'quux')", () => {
                shouldFail({
                    op: () => assert.isRejected(promise, "quux"),
                    message: "to be rejected with"
                });
            });

            // Chai 3.5.0 never interprets the 3rd parameter to assert.throws as
            // a custom error message. This is what we are checking here.
            describe(".isRejected(promise, 'quux', custom)", () => {
                shouldFail({
                    op: () => assert.isRejected(promise, "quux", custom),
                    notMessage: custom
                });
            });

            describe(".isRejected(promise, /quux/)", () => {
                shouldFail({
                    op: () => assert.isRejected(promise, /quux/),
                    message: "to be rejected with"
                });
            });
        });

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

            describe(".isRejected(promise, RangeError)", () => {
                shouldPass(() => assert.isRejected(promise, RangeError));
            });

            describe(".isRejected(promise, TypeError)", () => {
                shouldFail({
                    op: () => assert.isRejected(promise, TypeError),
                    message: "to be rejected"
                });
            });
        });

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

            describe(".isRejected(promise, RangeError, 'foo')", () => {
                shouldPass(() => assert.isRejected(promise, RangeError, "foo"));
            });

            describe(".isRejected(promise, RangeError, /bar/)", () => {
                shouldPass(() => assert.isRejected(promise, RangeError, /bar/));
            });

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

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

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

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

            describe(".isRejected(promise, RangeError, 'foo', custom)", () => {
                shouldPass(() => assert.isRejected(promise, RangeError, "foo", custom));
            });

            describe(".isRejected(promise, RangeError, /bar/, custom)", () => {
                shouldPass(() => assert.isRejected(promise, RangeError, /bar/, custom));
            });

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

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

            describe(".isRejected(promise, RangeError, undefined, custom)", () => {
                shouldPass(() => assert.isRejected(promise, RangeError, undefined, custom));
            });

            describe(".isRejected(promise, TypeError, undefined, custom)", () => {
                shouldFail({
                    op: () => assert.isRejected(promise, TypeError, undefined, custom),
                    message: custom
                });
            });
        });
    });
});