Skip to content

Commit 76af4f0

Browse files
BridgeARtargos
authored andcommitted
tools: prohibit assert.doesNotReject() in Node.js core
This makes sure we do not use `assert.doesNotReject()` anywhere in our code base. This is just a simple wrapper that catches the rejection and then rejects it again in case of an error. Thus, it is not useful to do that. The error message for `assert.doesNotThrow()` is also improved. PR-URL: #27402 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Beth Griggs <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 36d6168 commit 76af4f0

File tree

4 files changed

+43
-38
lines changed

4 files changed

+43
-38
lines changed

.eslintrc.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ module.exports = {
184184
},
185185
{
186186
selector: "CallExpression[callee.property.name='doesNotThrow']",
187-
message: 'Please replace `assert.doesNotThrow()` and add a comment next to the code instead.',
187+
message: 'Do not use `assert.doesNotThrow()`. Write the code without the wrapper and add a comment instead.',
188+
},
189+
{
190+
selector: "CallExpression[callee.property.name='doesNotReject']",
191+
message: 'Do not use `assert.doesNotReject()`. Write the code without the wrapper and add a comment instead.',
188192
},
189193
{
190194
selector: "CallExpression[callee.property.name='rejects'][arguments.length<2]",

doc/api/assert.md

+2
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ function. See [`assert.throws()`][] for more details.
454454
Besides the async nature to await the completion behaves identically to
455455
[`assert.doesNotThrow()`][].
456456

457+
<!-- eslint-disable no-restricted-syntax -->
457458
```js
458459
(async () => {
459460
await assert.doesNotReject(
@@ -465,6 +466,7 @@ Besides the async nature to await the completion behaves identically to
465466
})();
466467
```
467468

469+
<!-- eslint-disable no-restricted-syntax -->
468470
```js
469471
assert.doesNotReject(Promise.reject(new TypeError('Wrong value')))
470472
.then(() => {

test/parallel/test-assert-async.js

+34-34
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,36 @@ promises.push(assert.rejects(
114114
}
115115
));
116116

117+
{
118+
const handler = (generated, actual, err) => {
119+
assert.strictEqual(err.generatedMessage, generated);
120+
assert.strictEqual(err.code, 'ERR_ASSERTION');
121+
assert.strictEqual(err.actual, actual);
122+
assert.strictEqual(err.operator, 'rejects');
123+
assert(/rejects/.test(err.stack));
124+
return true;
125+
};
126+
const err = new Error();
127+
promises.push(assert.rejects(
128+
assert.rejects(Promise.reject(null), { code: 'FOO' }),
129+
handler.bind(null, true, null)
130+
));
131+
promises.push(assert.rejects(
132+
assert.rejects(Promise.reject(5), { code: 'FOO' }, 'AAAAA'),
133+
handler.bind(null, false, 5)
134+
));
135+
promises.push(assert.rejects(
136+
assert.rejects(Promise.reject(err), { code: 'FOO' }, 'AAAAA'),
137+
handler.bind(null, false, err)
138+
));
139+
}
140+
117141
// Check `assert.doesNotReject`.
118142
{
119143
// `assert.doesNotReject` accepts a function or a promise
120144
// or a thenable as first argument.
121-
const promise = assert.doesNotReject(() => new Map(), common.mustNotCall());
145+
/* eslint-disable no-restricted-syntax */
146+
let promise = assert.doesNotReject(() => new Map(), common.mustNotCall());
122147
promises.push(assert.rejects(promise, {
123148
message: 'Expected instance of Promise to be returned ' +
124149
'from the "promiseFn" function but got instance of Map.',
@@ -149,9 +174,7 @@ promises.push(assert.rejects(
149174
code: 'ERR_INVALID_RETURN_VALUE'
150175
})
151176
);
152-
}
153177

154-
{
155178
const handler1 = (err) => {
156179
assert(err instanceof assert.AssertionError,
157180
`${err.name} is not instance of AssertionError`);
@@ -173,47 +196,24 @@ promises.push(assert.rejects(
173196

174197
const rejectingFn = async () => assert.fail();
175198

176-
let promise = assert.doesNotReject(rejectingFn, common.mustCall(handler1));
199+
promise = assert.doesNotReject(rejectingFn, common.mustCall(handler1));
177200
promises.push(assert.rejects(promise, common.mustCall(handler2)));
178201

179202
promise = assert.doesNotReject(rejectingFn(), common.mustCall(handler1));
180203
promises.push(assert.rejects(promise, common.mustCall(handler2)));
181204

182205
promise = assert.doesNotReject(() => assert.fail(), common.mustNotCall());
183206
promises.push(assert.rejects(promise, common.mustCall(handler1)));
184-
}
185207

186-
promises.push(assert.rejects(
187-
assert.doesNotReject(123),
188-
{
189-
code: 'ERR_INVALID_ARG_TYPE',
190-
message: 'The "promiseFn" argument must be one of type ' +
191-
'Function or Promise. Received type number'
192-
}
193-
));
194-
195-
{
196-
const handler = (generated, actual, err) => {
197-
assert.strictEqual(err.generatedMessage, generated);
198-
assert.strictEqual(err.code, 'ERR_ASSERTION');
199-
assert.strictEqual(err.actual, actual);
200-
assert.strictEqual(err.operator, 'rejects');
201-
assert(/rejects/.test(err.stack));
202-
return true;
203-
};
204-
const err = new Error();
205208
promises.push(assert.rejects(
206-
assert.rejects(Promise.reject(null), { code: 'FOO' }),
207-
handler.bind(null, true, null)
208-
));
209-
promises.push(assert.rejects(
210-
assert.rejects(Promise.reject(5), { code: 'FOO' }, 'AAAAA'),
211-
handler.bind(null, false, 5)
212-
));
213-
promises.push(assert.rejects(
214-
assert.rejects(Promise.reject(err), { code: 'FOO' }, 'AAAAA'),
215-
handler.bind(null, false, err)
209+
assert.doesNotReject(123),
210+
{
211+
code: 'ERR_INVALID_ARG_TYPE',
212+
message: 'The "promiseFn" argument must be one of type ' +
213+
'Function or Promise. Received type number'
214+
}
216215
));
216+
/* eslint-enable no-restricted-syntax */
217217
}
218218

219219
// Make sure all async code gets properly executed.

test/parallel/test-fs-readdir-types.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,12 @@ fs.readdir(readdirDir, {
7272
assertDirents(dirents);
7373
}));
7474

75-
// Check the promisified version
76-
assert.doesNotReject(async () => {
75+
(async () => {
7776
const dirents = await fs.promises.readdir(readdirDir, {
7877
withFileTypes: true
7978
});
8079
assertDirents(dirents);
81-
});
80+
})();
8281

8382
// Check for correct types when the binding returns unknowns
8483
const UNKNOWN = constants.UV_DIRENT_UNKNOWN;

0 commit comments

Comments
 (0)