test: cover thenables when we check for promises · nodejs/node@2a51ae4 (original) (raw)
`@@ -2,12 +2,34 @@
`
2
2
`const common = require('../common');
`
3
3
`const assert = require('assert');
`
4
4
``
5
``
`-
// Test assert.rejects() and assert.doesNotReject() by checking their
`
6
``
`-
// expected output and by verifying that they do not work sync
`
7
``
-
8
5
`// Run all tests in parallel and check their outcome at the end.
`
9
6
`const promises = [];
`
10
7
``
``
8
`` +
// Thenable object without catch
method,
``
``
9
`+
// shouldn't be considered as a valid Thenable
`
``
10
`+
const invalidThenable = {
`
``
11
`+
then: (fulfill, reject) => {
`
``
12
`+
fulfill();
`
``
13
`+
},
`
``
14
`+
};
`
``
15
+
``
16
`+
// Function that returns a Thenable function,
`
``
17
`` +
// a function with catch
and then
methods attached,
``
``
18
`+
// shouldn't be considered as a valid Thenable.
`
``
19
`+
const invalidThenableFunc = () => {
`
``
20
`+
function f() {}
`
``
21
+
``
22
`+
f.then = (fulfill, reject) => {
`
``
23
`+
fulfill();
`
``
24
`+
};
`
``
25
`+
f.catch = () => {};
`
``
26
+
``
27
`+
return f;
`
``
28
`+
};
`
``
29
+
``
30
`+
// Test assert.rejects() and assert.doesNotReject() by checking their
`
``
31
`+
// expected output and by verifying that they do not work sync
`
``
32
+
11
33
`` // Check assert.rejects
.
``
12
34
`{
`
13
35
`const rejectingFn = async () => assert.fail();
`
`@@ -16,9 +38,34 @@ const promises = [];
`
16
38
`name: 'AssertionError',
`
17
39
`message: 'Failed'
`
18
40
`};
`
19
``
`` -
// assert.rejects
accepts a function or a promise as first argument.
``
``
41
+
``
42
`` +
// assert.rejects
accepts a function or a promise
``
``
43
`+
// or a thenable as first argument.
`
20
44
`promises.push(assert.rejects(rejectingFn, errObj));
`
21
45
`promises.push(assert.rejects(rejectingFn(), errObj));
`
``
46
+
``
47
`+
const validRejectingThenable = {
`
``
48
`+
then: (fulfill, reject) => {
`
``
49
`+
reject({ code: 'FAIL' });
`
``
50
`+
},
`
``
51
`+
catch: () => {}
`
``
52
`+
};
`
``
53
`+
promises.push(assert.rejects(validRejectingThenable, { code: 'FAIL' }));
`
``
54
+
``
55
`` +
// assert.rejects
should not accept thenables that
``
``
56
`` +
// use a function as obj
and that have no catch
handler.
``
``
57
`+
promises.push(assert.rejects(
`
``
58
`+
assert.rejects(invalidThenable, {}),
`
``
59
`+
{
`
``
60
`+
code: 'ERR_INVALID_ARG_TYPE'
`
``
61
`+
})
`
``
62
`+
);
`
``
63
`+
promises.push(assert.rejects(
`
``
64
`+
assert.rejects(invalidThenableFunc, {}),
`
``
65
`+
{
`
``
66
`+
code: 'ERR_INVALID_RETURN_VALUE'
`
``
67
`+
})
`
``
68
`+
);
`
22
69
`}
`
23
70
``
24
71
`{
`
`@@ -69,7 +116,8 @@ promises.push(assert.rejects(
`
69
116
``
70
117
`` // Check assert.doesNotReject
.
``
71
118
`{
`
72
``
`` -
// assert.doesNotReject
accepts a function or a promise as first argument.
``
``
119
`` +
// assert.doesNotReject
accepts a function or a promise
``
``
120
`+
// or a thenable as first argument.
`
73
121
`const promise = assert.doesNotReject(() => new Map(), common.mustNotCall());
`
74
122
`promises.push(assert.rejects(promise, {
`
75
123
`message: 'Expected instance of Promise to be returned ' +
`
`@@ -79,6 +127,28 @@ promises.push(assert.rejects(
`
79
127
`}));
`
80
128
`promises.push(assert.doesNotReject(async () => {}));
`
81
129
`promises.push(assert.doesNotReject(Promise.resolve()));
`
``
130
+
``
131
`` +
// assert.doesNotReject
should not accept thenables that
``
``
132
`` +
// use a function as obj
and that have no catch
handler.
``
``
133
`+
const validFulfillingThenable = {
`
``
134
`+
then: (fulfill, reject) => {
`
``
135
`+
fulfill();
`
``
136
`+
},
`
``
137
`+
catch: () => {}
`
``
138
`+
};
`
``
139
`+
promises.push(assert.doesNotReject(validFulfillingThenable));
`
``
140
`+
promises.push(assert.rejects(
`
``
141
`+
assert.doesNotReject(invalidThenable),
`
``
142
`+
{
`
``
143
`+
code: 'ERR_INVALID_ARG_TYPE'
`
``
144
`+
})
`
``
145
`+
);
`
``
146
`+
promises.push(assert.rejects(
`
``
147
`+
assert.doesNotReject(invalidThenableFunc),
`
``
148
`+
{
`
``
149
`+
code: 'ERR_INVALID_RETURN_VALUE'
`
``
150
`+
})
`
``
151
`+
);
`
82
152
`}
`
83
153
``
84
154
`{
`