Skip to content

Commit 6c59114

Browse files
aduh95UlisesGascon
authored andcommitted
test: ensure never settling promises are detected
PR-URL: #50318 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 11412e8 commit 6c59114

40 files changed

+147
-143
lines changed

test/.eslintrc.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ rules:
4949
message: Use 'test' as debuglog value in tests.
5050
- selector: CallExpression:matches([callee.object.name="common"][callee.property.name=/^must(Not)?Call/],[callee.name="mustCall"],[callee.name="mustCallAtLeast"],[callee.name="mustNotCall"])>:first-child[type=/FunctionExpression$/][body.body.length=0]
5151
message: Do not use an empty function, omit the parameter altogether.
52+
- selector: ExpressionStatement>CallExpression:matches([callee.name='rejects'], [callee.object.name='assert'][callee.property.name='rejects'])
53+
message: Calling `assert.rejects` without `await` or `.then(common.mustCall())` will not detect never-settling promises.
5254
- selector: Identifier[name='webcrypto']
5355
message: Use `globalThis.crypto`.
5456

test/es-module/test-esm-cjs-named-error.mjs

+8-8
Original file line numberDiff line numberDiff line change
@@ -23,55 +23,55 @@ const expectedPackageHack =
2323

2424
const expectedBare = errTemplate('deep-fail', 'comeOn', '{ comeOn }');
2525

26-
rejects(async () => {
26+
await rejects(async () => {
2727
await import(`${fixtureBase}/single-quote.mjs`);
2828
}, {
2929
name: 'SyntaxError',
3030
message: expectedRelative
3131
}, 'should support relative specifiers with single quotes');
3232

33-
rejects(async () => {
33+
await rejects(async () => {
3434
await import(`${fixtureBase}/double-quote.mjs`);
3535
}, {
3636
name: 'SyntaxError',
3737
message: expectedRelative
3838
}, 'should support relative specifiers with double quotes');
3939

40-
rejects(async () => {
40+
await rejects(async () => {
4141
await import(`${fixtureBase}/renamed-import.mjs`);
4242
}, {
4343
name: 'SyntaxError',
4444
message: expectedRenamed
4545
}, 'should correctly format named imports with renames');
4646

47-
rejects(async () => {
47+
await rejects(async () => {
4848
await import(`${fixtureBase}/multi-line.mjs`);
4949
}, {
5050
name: 'SyntaxError',
5151
message: expectedWithoutExample,
5252
}, 'should correctly format named imports across multiple lines');
5353

54-
rejects(async () => {
54+
await rejects(async () => {
5555
await import(`${fixtureBase}/json-hack.mjs`);
5656
}, {
5757
name: 'SyntaxError',
5858
message: expectedPackageHack
5959
}, 'should respect recursive package.json for module type');
6060

61-
rejects(async () => {
61+
await rejects(async () => {
6262
await import(`${fixtureBase}/bare-import-single.mjs`);
6363
}, {
6464
name: 'SyntaxError',
6565
message: expectedBare
6666
}, 'should support bare specifiers with single quotes');
6767

68-
rejects(async () => {
68+
await rejects(async () => {
6969
await import(`${fixtureBase}/bare-import-double.mjs`);
7070
}, {
7171
name: 'SyntaxError',
7272
message: expectedBare
7373
}, 'should support bare specifiers with double quotes');
7474

75-
rejects(async () => {
75+
await rejects(async () => {
7676
await import(`${fixtureBase}/escaped-single-quote.mjs`);
7777
}, /import pkg from '\.\/oh'no\.cjs'/, 'should support relative specifiers with escaped single quote');

test/internet/test-dns-lookup.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ assert.rejects(
1717
code: 'ENOTFOUND',
1818
message: `getaddrinfo ENOTFOUND ${addresses.NOT_FOUND}`,
1919
},
20-
);
20+
).then(common.mustCall());
2121

2222
assert.rejects(
2323
dnsPromises.lookup(addresses.NOT_FOUND, {
@@ -29,7 +29,7 @@ assert.rejects(
2929
code: 'ENOTFOUND',
3030
message: `getaddrinfo ENOTFOUND ${addresses.NOT_FOUND}`,
3131
},
32-
);
32+
).then(common.mustCall());
3333

3434
dns.lookup(addresses.NOT_FOUND, {
3535
hints: 0,

test/parallel/test-blob.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,10 @@ assert.throws(() => new Blob({}), {
409409
}
410410

411411
(async () => {
412-
assert.rejects(async () => Blob.prototype.arrayBuffer.call(), {
412+
await assert.rejects(async () => Blob.prototype.arrayBuffer.call(), {
413413
code: 'ERR_INVALID_THIS',
414414
});
415-
assert.rejects(async () => Blob.prototype.text.call(), {
415+
await assert.rejects(async () => Blob.prototype.text.call(), {
416416
code: 'ERR_INVALID_THIS',
417417
});
418418
})().then(common.mustCall());

test/parallel/test-crypto-webcrypto-aes-decrypt-tag-too-small.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ subtle.importKey(
1616
},
1717
false,
1818
[ 'encrypt', 'decrypt' ])
19-
.then((k) => {
19+
.then((k) =>
2020
assert.rejects(() => {
2121
return subtle.decrypt({
2222
name: 'AES-GCM',
@@ -25,5 +25,5 @@ subtle.importKey(
2525
}, {
2626
name: 'OperationError',
2727
message: /The provided data is too small/,
28-
});
29-
});
28+
})
29+
).then(common.mustCall());
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const fixtures = require('../common/fixtures');
55
const assert = require('assert');
66
const { pathToFileURL } = require('url');
77

88
{
9-
assert.rejects(import('./'), /ERR_UNSUPPORTED_DIR_IMPORT/);
9+
assert.rejects(import('./'), /ERR_UNSUPPORTED_DIR_IMPORT/).then(common.mustCall());
1010
assert.rejects(
1111
import(pathToFileURL(fixtures.path('packages', 'main'))),
1212
/Did you mean/,
13-
);
13+
).then(common.mustCall());
1414
}

test/parallel/test-dns-lookup.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,4 @@ tickValue = 1;
205205

206206
// Should fail due to stub.
207207
assert.rejects(dnsPromises.lookup('example.com'),
208-
{ code: 'ENOMEM', hostname: 'example.com' });
208+
{ code: 'ENOMEM', hostname: 'example.com' }).then(common.mustCall());

test/parallel/test-dns-lookupService-promises.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ dnsPromises.lookupService('127.0.0.1', 22).then(common.mustCall((result) => {
1616
assert.rejects(
1717
() => dnsPromises.lookupService('192.0.2.1', 22),
1818
{ code: /^(?:ENOTFOUND|EAI_AGAIN)$/ }
19-
);
19+
).then(common.mustCall());

test/parallel/test-dns-lookupService.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ assert.rejects(
3232
message: 'getnameinfo ENOENT 127.0.0.1',
3333
syscall: 'getnameinfo'
3434
}
35-
);
35+
).then(common.mustCall());

test/parallel/test-dns-resolve-promises.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Flags: --expose-internals
22
'use strict';
3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55
const { internalBinding } = require('internal/test/binding');
66
const cares = internalBinding('cares_wrap');
@@ -17,4 +17,4 @@ assert.rejects(
1717
syscall: 'queryA',
1818
hostname: 'example.org'
1919
}
20-
);
20+
).then(common.mustCall());

test/parallel/test-event-emitter-error-monitor.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ EE.emit('error', theErr);
2525

2626
// Verify it works with once
2727
process.nextTick(() => EE.emit('error', theErr));
28-
assert.rejects(EventEmitter.once(EE, 'notTriggered'), theErr);
28+
assert.rejects(EventEmitter.once(EE, 'notTriggered'), theErr).then(common.mustCall());
2929

3030
// Only error events trigger error monitor
3131
EE.on('aEvent', common.mustCall());

test/parallel/test-file-validate-mode-flag.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ assert.throws(() => openSync(__filename, 0, invalid), {
3333

3434
assert.rejects(openPromise(__filename, invalid), {
3535
code: 'ERR_OUT_OF_RANGE'
36-
});
36+
}).then(common.mustCall());
3737

3838
assert.rejects(openPromise(__filename, 0, invalid), {
3939
code: 'ERR_OUT_OF_RANGE'
40-
});
40+
}).then(common.mustCall());

test/parallel/test-filehandle-close.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const fs = require('fs');
1010
const fh = await fs.promises.open(__filename);
1111
fs.closeSync(fh.fd);
1212

13-
assert.rejects(() => fh.close(), {
13+
await assert.rejects(() => fh.close(), {
1414
code: 'EBADF',
1515
syscall: 'close'
1616
});

test/parallel/test-fs-filehandle-use-after-close.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const fs = require('fs').promises;
1616
// See https://github.com/nodejs/node/issues/31361 for more details.
1717
const otherFilehandle = await fs.open(process.execPath);
1818

19-
assert.rejects(() => filehandle.stat(), {
19+
await assert.rejects(() => filehandle.stat(), {
2020
code: 'EBADF',
2121
syscall: 'fstat'
2222
});

test/parallel/test-fs-lchmod.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ assert.throws(() => fs.lchmod(f, {}), { code: 'ERR_INVALID_ARG_TYPE' });
4242
code: 'ERR_INVALID_ARG_TYPE',
4343
};
4444

45-
assert.rejects(promises.lchmod(f, input, () => {}), errObj);
45+
assert.rejects(promises.lchmod(f, input, () => {}), errObj).then(common.mustCall());
4646
assert.throws(() => fs.lchmodSync(f, input), errObj);
4747
});
4848

@@ -61,6 +61,6 @@ assert.throws(() => fs.lchmodSync(f, '123x'), {
6161
`4294967295. Received ${input}`
6262
};
6363

64-
assert.rejects(promises.lchmod(f, input, () => {}), errObj);
64+
assert.rejects(promises.lchmod(f, input, () => {}), errObj).then(common.mustCall());
6565
assert.throws(() => fs.lchmodSync(f, input), errObj);
6666
});

test/parallel/test-fs-open.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ for (const extra of [[], ['r'], ['r', 0], ['r', 0, 'bad callback']]) {
9494
code: 'ERR_INVALID_ARG_TYPE',
9595
name: 'TypeError'
9696
}
97-
);
97+
).then(common.mustCall());
9898
});
9999

100100
// Check invalid modes.
@@ -116,5 +116,5 @@ for (const extra of [[], ['r'], ['r', 0], ['r', 0, 'bad callback']]) {
116116
{
117117
code: 'ERR_INVALID_ARG_TYPE'
118118
}
119-
);
119+
).then(common.mustCall());
120120
});

test/parallel/test-fs-promises-readfile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function validateReadFileAbortLogicBefore() {
4646
const signal = AbortSignal.abort();
4747
assert.rejects(readFile(fn, { signal }), {
4848
name: 'AbortError'
49-
});
49+
}).then(common.mustCall());
5050
}
5151

5252
function validateReadFileAbortLogicDuring() {
@@ -55,7 +55,7 @@ function validateReadFileAbortLogicDuring() {
5555
process.nextTick(() => controller.abort());
5656
assert.rejects(readFile(fn, { signal }), {
5757
name: 'AbortError'
58-
});
58+
}).then(common.mustCall());
5959
}
6060

6161
async function validateWrongSignalParam() {

test/parallel/test-fs-promises-watch.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -79,42 +79,42 @@ assert.rejects(
7979
// eslint-disable-next-line no-unused-vars, no-empty
8080
for await (const _ of watch(1)) { }
8181
},
82-
{ code: 'ERR_INVALID_ARG_TYPE' });
82+
{ code: 'ERR_INVALID_ARG_TYPE' }).then(common.mustCall());
8383

8484
assert.rejects(
8585
async () => {
8686
// eslint-disable-next-line no-unused-vars, no-empty
8787
for await (const _ of watch(__filename, 1)) { }
8888
},
89-
{ code: 'ERR_INVALID_ARG_TYPE' });
89+
{ code: 'ERR_INVALID_ARG_TYPE' }).then(common.mustCall());
9090

9191
assert.rejects(
9292
async () => {
9393
// eslint-disable-next-line no-unused-vars, no-empty
9494
for await (const _ of watch('', { persistent: 1 })) { }
9595
},
96-
{ code: 'ERR_INVALID_ARG_TYPE' });
96+
{ code: 'ERR_INVALID_ARG_TYPE' }).then(common.mustCall());
9797

9898
assert.rejects(
9999
async () => {
100100
// eslint-disable-next-line no-unused-vars, no-empty
101101
for await (const _ of watch('', { recursive: 1 })) { }
102102
},
103-
{ code: 'ERR_INVALID_ARG_TYPE' });
103+
{ code: 'ERR_INVALID_ARG_TYPE' }).then(common.mustCall());
104104

105105
assert.rejects(
106106
async () => {
107107
// eslint-disable-next-line no-unused-vars, no-empty
108108
for await (const _ of watch('', { encoding: 1 })) { }
109109
},
110-
{ code: 'ERR_INVALID_ARG_VALUE' });
110+
{ code: 'ERR_INVALID_ARG_VALUE' }).then(common.mustCall());
111111

112112
assert.rejects(
113113
async () => {
114114
// eslint-disable-next-line no-unused-vars, no-empty
115115
for await (const _ of watch('', { signal: 1 })) { }
116116
},
117-
{ code: 'ERR_INVALID_ARG_TYPE' });
117+
{ code: 'ERR_INVALID_ARG_TYPE' }).then(common.mustCall());
118118

119119
(async () => {
120120
const ac = new AbortController();

test/parallel/test-fs-promises.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,21 @@ assert.strictEqual(
5959
name: 'Error',
6060
message: /^ENOENT: no such file or directory, access/
6161
}
62-
);
62+
).then(common.mustCall());
6363

6464
assert.rejects(
6565
access(__filename, 8),
6666
{
6767
code: 'ERR_OUT_OF_RANGE',
6868
}
69-
);
69+
).then(common.mustCall());
7070

7171
assert.rejects(
7272
access(__filename, { [Symbol.toPrimitive]() { return 5; } }),
7373
{
7474
code: 'ERR_INVALID_ARG_TYPE',
7575
}
76-
);
76+
).then(common.mustCall());
7777
}
7878

7979
function verifyStatObject(stat) {
@@ -407,7 +407,7 @@ async function executeOnHandle(dest, func) {
407407
const dir = path.join(tmpDir, nextdir(), nextdir());
408408
await mkdir(path.dirname(dir));
409409
await writeFile(dir, '');
410-
assert.rejects(
410+
await assert.rejects(
411411
mkdir(dir, { recursive: true }),
412412
{
413413
code: 'EEXIST',
@@ -424,7 +424,7 @@ async function executeOnHandle(dest, func) {
424424
const dir = path.join(file, nextdir(), nextdir());
425425
await mkdir(path.dirname(file));
426426
await writeFile(file, '');
427-
assert.rejects(
427+
await assert.rejects(
428428
mkdir(dir, { recursive: true }),
429429
{
430430
code: 'ENOTDIR',
@@ -463,14 +463,14 @@ async function executeOnHandle(dest, func) {
463463
code: 'ERR_INVALID_ARG_TYPE',
464464
name: 'TypeError'
465465
}
466-
);
466+
).then(common.mustCall());
467467
});
468468
}
469469

470470
// `mkdtemp` with invalid numeric prefix
471471
{
472472
await mkdtemp(path.resolve(tmpDir, 'FOO'));
473-
assert.rejects(
473+
await assert.rejects(
474474
// mkdtemp() expects to get a string prefix.
475475
async () => mkdtemp(1),
476476
{

test/parallel/test-fs-read-empty-buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ assert.throws(
3737
message: 'The argument \'buffer\' is empty and cannot be written. ' +
3838
'Received Uint8Array(0) []'
3939
}
40-
);
40+
).then(common.mustCall());
4141
})().then(common.mustCall());

0 commit comments

Comments
 (0)