Skip to content

Commit 9671826

Browse files
cjihrigjasnell
authored andcommitted
test_runner: remove promises returned by test()
This commit updates the test() and suite() APIs to no longer return a Promise. Fixes: #51292 PR-URL: #56664 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Pietro Marchini <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Raz Luvaton <[email protected]> Reviewed-By: Chemi Atlow <[email protected]>
1 parent aa3523e commit 9671826

File tree

5 files changed

+23
-47
lines changed

5 files changed

+23
-47
lines changed

doc/api/test.md

+9-23
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,11 @@ run({ files: [path.resolve('./tests/test.js')] })
14071407
added:
14081408
- v22.0.0
14091409
- v20.13.0
1410+
changes:
1411+
- version:
1412+
- REPLACEME
1413+
pr-url: https://github.com/nodejs/node/pull/56664
1414+
description: This function no longer returns a `Promise`.
14101415
-->
14111416

14121417
* `name` {string} The name of the suite, which is displayed when reporting test
@@ -1417,7 +1422,6 @@ added:
14171422
* `fn` {Function|AsyncFunction} The suite function declaring nested tests and
14181423
suites. The first argument to this function is a [`SuiteContext`][] object.
14191424
**Default:** A no-op function.
1420-
* Returns: {Promise} Immediately fulfilled with `undefined`.
14211425

14221426
The `suite()` function is imported from the `node:test` module.
14231427

@@ -1461,6 +1465,10 @@ added:
14611465
- v18.0.0
14621466
- v16.17.0
14631467
changes:
1468+
- version:
1469+
- REPLACEME
1470+
pr-url: https://github.com/nodejs/node/pull/56664
1471+
description: This function no longer returns a `Promise`.
14641472
- version:
14651473
- v20.2.0
14661474
- v18.17.0
@@ -1510,8 +1518,6 @@ changes:
15101518
to this function is a [`TestContext`][] object. If the test uses callbacks,
15111519
the callback function is passed as the second argument. **Default:** A no-op
15121520
function.
1513-
* Returns: {Promise} Fulfilled with `undefined` once
1514-
the test completes, or immediately if the test runs within a suite.
15151521

15161522
The `test()` function is the value imported from the `test` module. Each
15171523
invocation of this function results in reporting the test to the {TestsStream}.
@@ -1520,26 +1526,6 @@ The `TestContext` object passed to the `fn` argument can be used to perform
15201526
actions related to the current test. Examples include skipping the test, adding
15211527
additional diagnostic information, or creating subtests.
15221528

1523-
`test()` returns a `Promise` that fulfills once the test completes.
1524-
if `test()` is called within a suite, it fulfills immediately.
1525-
The return value can usually be discarded for top level tests.
1526-
However, the return value from subtests should be used to prevent the parent
1527-
test from finishing first and cancelling the subtest
1528-
as shown in the following example.
1529-
1530-
```js
1531-
test('top level test', async (t) => {
1532-
// The setTimeout() in the following subtest would cause it to outlive its
1533-
// parent test if 'await' is removed on the next line. Once the parent test
1534-
// completes, it will cancel any outstanding subtests.
1535-
await t.test('longer running subtest', async (t) => {
1536-
return new Promise((resolve, reject) => {
1537-
setTimeout(resolve, 1000);
1538-
});
1539-
});
1540-
});
1541-
```
1542-
15431529
The `timeout` option can be used to fail the test if it takes longer than
15441530
`timeout` milliseconds to complete. However, it is not a reliable mechanism for
15451531
canceling tests because a running test might block the application thread and

lib/internal/test_runner/harness.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,12 @@ function runInParentContext(Factory) {
312312
function run(name, options, fn, overrides) {
313313
const parent = testResources.get(executionAsyncId()) || lazyBootstrapRoot();
314314
const subtest = parent.createSubtest(Factory, name, options, fn, overrides);
315+
315316
if (parent instanceof Suite) {
316-
return PromiseResolve();
317+
return;
317318
}
318319

319-
return startSubtestAfterBootstrap(subtest);
320+
startSubtestAfterBootstrap(subtest);
320321
}
321322

322323
const test = (name, options, fn) => {
@@ -325,7 +326,7 @@ function runInParentContext(Factory) {
325326
loc: getCallerLocation(),
326327
};
327328

328-
return run(name, options, fn, overrides);
329+
run(name, options, fn, overrides);
329330
};
330331
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
331332
test[keyword] = (name, options, fn) => {
@@ -335,7 +336,7 @@ function runInParentContext(Factory) {
335336
loc: getCallerLocation(),
336337
};
337338

338-
return run(name, options, fn, overrides);
339+
run(name, options, fn, overrides);
339340
};
340341
});
341342
return test;

test/parallel/test-runner-coverage-source-map.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,4 @@ describe('Coverage with source maps', async () => {
122122
t.assert.strictEqual(spawned.code, 1);
123123
});
124124
}
125-
}).then(common.mustCall());
125+
});

test/parallel/test-runner-misc.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ if (process.argv[2] === 'child') {
1818
assert.strictEqual(signal.aborted, false);
1919
testSignal = signal;
2020
await setTimeout(50);
21-
})).finally(common.mustCall(() => {
22-
test(() => assert.strictEqual(testSignal.aborted, true));
2321
}));
22+
test(() => assert.strictEqual(testSignal.aborted, true));
2423

2524
// TODO(benjamingr) add more tests to describe + AbortSignal
2625
// this just tests the parameter is passed

test/parallel/test-runner-typechecking.js

+7-17
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require('../common');
66

77
const assert = require('assert');
88
const { test, describe, it } = require('node:test');
9-
const { isPromise } = require('util/types');
109

1110
const testOnly = test('only test', { only: true });
1211
const testTodo = test('todo test', { todo: true });
@@ -16,21 +15,12 @@ const testTodoShorthand = test.todo('todo test shorthand');
1615
const testSkipShorthand = test.skip('skip test shorthand');
1716

1817
describe('\'node:test\' and its shorthands should return the same', () => {
19-
it('should return a Promise', () => {
20-
assert(isPromise(testOnly));
21-
assert(isPromise(testTodo));
22-
assert(isPromise(testSkip));
23-
assert(isPromise(testOnlyShorthand));
24-
assert(isPromise(testTodoShorthand));
25-
assert(isPromise(testSkipShorthand));
26-
});
27-
28-
it('should resolve undefined', async () => {
29-
assert.strictEqual(await testOnly, undefined);
30-
assert.strictEqual(await testTodo, undefined);
31-
assert.strictEqual(await testSkip, undefined);
32-
assert.strictEqual(await testOnlyShorthand, undefined);
33-
assert.strictEqual(await testTodoShorthand, undefined);
34-
assert.strictEqual(await testSkipShorthand, undefined);
18+
it('should return undefined', () => {
19+
assert.strictEqual(testOnly, undefined);
20+
assert.strictEqual(testTodo, undefined);
21+
assert.strictEqual(testSkip, undefined);
22+
assert.strictEqual(testOnlyShorthand, undefined);
23+
assert.strictEqual(testTodoShorthand, undefined);
24+
assert.strictEqual(testSkipShorthand, undefined);
3525
});
3626
});

0 commit comments

Comments
 (0)