tools,test: enforce best practices to detect never settling promises

PR-URL: https://github.com/nodejs/node/pull/58992
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Antoine du Hamel 2025-07-16 10:57:22 +02:00 committed by GitHub
parent 9f486c35fc
commit 48fff6baf8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 17 additions and 13 deletions

View file

@ -14,4 +14,4 @@ const assert = require('assert');
}, {
message: 'test',
});
})().catch(common.mustNotCall());
})().then(common.mustCall());

View file

@ -13,4 +13,4 @@ assert.throws(() => {
assert.rejects(import('../fixtures/es-modules/throw-error.mjs'), {
message: 'test',
}).catch(common.mustNotCall());
}).then(common.mustCall());

View file

@ -23,4 +23,4 @@ async function test() {
}
// Run the test twice to check consistency after caching.
test().then(common.mustCall(test)).catch(common.mustNotCall());
test().then(common.mustCall(test));

View file

@ -22,4 +22,4 @@ async function test() {
}
// Run the test twice to check consistency after caching.
test().then(common.mustCall(test)).catch(common.mustNotCall());
test().then(common.mustCall(test));

View file

@ -70,4 +70,4 @@ async function main() {
}
}
main().catch(common.mustNotCall());
main().then(common.mustCall());

View file

@ -116,4 +116,4 @@ async function main() {
}
}
main().catch(common.mustNotCall());
main().then(common.mustCall());

View file

@ -92,6 +92,10 @@ export default [
selector: "ExpressionStatement>CallExpression:matches([callee.name='rejects'], [callee.object.name='assert'][callee.property.name='rejects'])",
message: 'Calling `assert.rejects` without `await` or `.then(common.mustCall())` will not detect never-settling promises.',
},
{
selector: 'CallExpression[callee.property.name="catch"]>:first-child:matches(CallExpression[callee.object.name="common"][callee.property.name="mustNotCall"], CallExpression[callee.name="mustNotCall"])',
message: 'Calling `.catch(common.mustNotCall())` will not detect never-settling promises. Use `.then(common.mustCall())` instead.',
},
],
// Stylistic rules.

View file

@ -48,4 +48,4 @@ function hook(code, filename) {
assert.deepStrictEqual({ ...bar }, { $key: 'bar-esm' });
}
})().catch(common.mustNotCall());
})().then(common.mustCall());

View file

@ -48,4 +48,4 @@ function hook(code, filename) {
assert.deepStrictEqual({ ...bar }, { $key: 'bar-esm' });
}
})().catch(common.mustNotCall());
})().then(common.mustCall());

View file

@ -138,9 +138,9 @@ test('dsa_public.pem', 'dsa_private.pem', 'sha256', false,
verify('sha256', data, publicKey, signature),
verify('sha256', data, publicKey, signature),
verify('sha256', data, publicKey, signature),
]).then(common.mustCall());
]);
})
.catch(common.mustNotCall());
.then(common.mustCall());
}
{

View file

@ -53,7 +53,7 @@ async function promise() {
await (await fs.promises.open(__filename, 'r')).close();
}
promise().then(common.mustCall()).catch(common.mustNotCall());
promise().then(common.mustCall());
assert.throws(
() => fs.open(__filename, 'r', 'boom', common.mustNotCall()),

View file

@ -690,7 +690,7 @@ async function tests() {
const it = r[Symbol.asyncIterator]();
const p = it.return();
r.emit('close');
p.then(common.mustCall()).catch(common.mustNotCall());
p.then(common.mustCall());
}
{

View file

@ -182,4 +182,4 @@ function checkFrozen(context) {
const promise = script.runInContext(context);
assert.strictEqual(await promise, namespace);
}
})().catch(common.mustNotCall());
})().then(common.mustCall());