Skip to content

Commit 1a2eb15

Browse files
cjihrigjasnell
authored andcommitted
test_runner: remove promises returned by t.test()
This commit updates the TestContext.prototype.test() API 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 9671826 commit 1a2eb15

11 files changed

+36
-67
lines changed

doc/api/test.md

+26-36
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ top level test with two subtests.
105105

106106
```js
107107
test('top level test', async (t) => {
108-
await t.test('subtest 1', (t) => {
108+
t.test('subtest 1', (t) => {
109109
assert.strictEqual(1, 1);
110110
});
111111

112-
await t.test('subtest 2', (t) => {
112+
t.test('subtest 2', (t) => {
113113
assert.strictEqual(2, 2);
114114
});
115115
});
@@ -118,12 +118,7 @@ test('top level test', async (t) => {
118118
> **Note:** `beforeEach` and `afterEach` hooks are triggered
119119
> between each subtest execution.
120120
121-
In this example, `await` is used to ensure that both subtests have completed.
122-
This is necessary because tests do not wait for their subtests to
123-
complete, unlike tests created within suites.
124-
Any subtests that are still outstanding when their parent finishes
125-
are cancelled and treated as failures. Any subtest failures cause the parent
126-
test to fail.
121+
Any subtest failures cause the parent test to fail.
127122

128123
## Skipping tests
129124

@@ -241,20 +236,20 @@ that are not executed are omitted from the test runner output.
241236
// The suite's 'only' option is set, so these tests are run.
242237
test('this test is run', { only: true }, async (t) => {
243238
// Within this test, all subtests are run by default.
244-
await t.test('running subtest');
239+
t.test('running subtest');
245240

246241
// The test context can be updated to run subtests with the 'only' option.
247242
t.runOnly(true);
248-
await t.test('this subtest is now skipped');
249-
await t.test('this subtest is run', { only: true });
243+
t.test('this subtest is now skipped');
244+
t.test('this subtest is run', { only: true });
250245

251246
// Switch the context back to execute all tests.
252247
t.runOnly(false);
253-
await t.test('this subtest is now run');
248+
t.test('this subtest is now run');
254249

255250
// Explicitly do not run these tests.
256-
await t.test('skipped subtest 3', { only: false });
257-
await t.test('skipped subtest 4', { skip: true });
251+
t.test('skipped subtest 3', { only: false });
252+
t.test('skipped subtest 4', { skip: true });
258253
});
259254

260255
// The 'only' option is not set, so this test is skipped.
@@ -309,13 +304,13 @@ multiple times (e.g. `--test-name-pattern="test 1"`,
309304

310305
```js
311306
test('test 1', async (t) => {
312-
await t.test('test 2');
313-
await t.test('test 3');
307+
t.test('test 2');
308+
t.test('test 3');
314309
});
315310

316311
test('Test 4', async (t) => {
317-
await t.test('Test 5');
318-
await t.test('test 6');
312+
t.test('Test 5');
313+
t.test('test 6');
319314
});
320315
```
321316

@@ -3175,12 +3170,9 @@ before each subtest of the current test.
31753170
```js
31763171
test('top level test', async (t) => {
31773172
t.beforeEach((t) => t.diagnostic(`about to run ${t.name}`));
3178-
await t.test(
3179-
'This is a subtest',
3180-
(t) => {
3181-
assert.ok('some relevant assertion here');
3182-
},
3183-
);
3173+
t.test('This is a subtest', (t) => {
3174+
assert.ok('some relevant assertion here');
3175+
});
31843176
});
31853177
```
31863178

@@ -3238,12 +3230,9 @@ after each subtest of the current test.
32383230
```js
32393231
test('top level test', async (t) => {
32403232
t.afterEach((t) => t.diagnostic(`finished running ${t.name}`));
3241-
await t.test(
3242-
'This is a subtest',
3243-
(t) => {
3244-
assert.ok('some relevant assertion here');
3245-
},
3246-
);
3233+
t.test('This is a subtest', (t) => {
3234+
assert.ok('some relevant assertion here');
3235+
});
32473236
});
32483237
```
32493238

@@ -3458,10 +3447,8 @@ no-op.
34583447
test('top level test', (t) => {
34593448
// The test context can be set to run subtests with the 'only' option.
34603449
t.runOnly(true);
3461-
return Promise.all([
3462-
t.test('this subtest is now skipped'),
3463-
t.test('this subtest is run', { only: true }),
3464-
]);
3450+
t.test('this subtest is now skipped');
3451+
t.test('this subtest is run', { only: true });
34653452
});
34663453
```
34673454

@@ -3533,6 +3520,10 @@ added:
35333520
- v18.0.0
35343521
- v16.17.0
35353522
changes:
3523+
- version:
3524+
- REPLACEME
3525+
pr-url: https://github.com/nodejs/node/pull/56664
3526+
description: This function no longer returns a `Promise`.
35363527
- version:
35373528
- v18.8.0
35383529
- v16.18.0
@@ -3577,14 +3568,13 @@ changes:
35773568
to this function is a [`TestContext`][] object. If the test uses callbacks,
35783569
the callback function is passed as the second argument. **Default:** A no-op
35793570
function.
3580-
* Returns: {Promise} Fulfilled with `undefined` once the test completes.
35813571

35823572
This function is used to create subtests under the current test. This function
35833573
behaves in the same fashion as the top level [`test()`][] function.
35843574

35853575
```js
35863576
test('top level test', async (t) => {
3587-
await t.test(
3577+
t.test(
35883578
'This is a subtest',
35893579
{ only: false, skip: false, concurrency: 1, todo: false, plan: 1 },
35903580
(t) => {

lib/internal/test_runner/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class TestContext {
304304
Test, name, options, fn, overrides,
305305
);
306306

307-
return subtest.start();
307+
subtest.start();
308308
}
309309

310310
before(fn, options) {

test/fixtures/test-runner/output/dot_reporter.snapshot

-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,6 @@ Failed tests:
154154
*
155155
*
156156
*
157-
*
158-
*
159157
✖ subtest sync throw fails (*ms)
160158
'2 subtests failed'
161159
✖ timed out async test (*ms)

test/fixtures/test-runner/output/hooks.snapshot

-5
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,6 @@ not ok 16 - t.after throws - no subtests
576576
*
577577
*
578578
*
579-
*
580579
...
581580
1..2
582581
not ok 17 - t.beforeEach throws
@@ -607,8 +606,6 @@ not ok 17 - t.beforeEach throws
607606
*
608607
*
609608
*
610-
*
611-
*
612609
...
613610
# Subtest: 2
614611
not ok 2 - 2
@@ -629,7 +626,6 @@ not ok 17 - t.beforeEach throws
629626
*
630627
*
631628
*
632-
*
633629
...
634630
1..2
635631
not ok 18 - t.afterEach throws
@@ -757,7 +753,6 @@ not ok 21 - afterEach context when test fails
757753
*
758754
*
759755
*
760-
*
761756
...
762757
1..2
763758
not ok 22 - afterEach throws and test fails

test/fixtures/test-runner/output/hooks_spec_reporter.snapshot

-5
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@
363363
*
364364
*
365365
*
366-
*
367366

368367
*
369368
1 (*ms)
@@ -376,8 +375,6 @@
376375
*
377376
*
378377
*
379-
*
380-
*
381378

382379
*
383380
2 (*ms)
@@ -391,7 +388,6 @@
391388
*
392389
*
393390
*
394-
*
395391

396392
*
397393
1 (*ms)
@@ -439,7 +435,6 @@
439435
*
440436
*
441437
*
442-
*
443438

444439
*
445440
t.after() is called if test body throws (*ms)

test/fixtures/test-runner/output/junit_reporter.snapshot

+1-4
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at first
351351
</testcase>
352352
<testcase name="sync throw fails at second" time="*" classname="test" failure="thrown from subtest sync throw fails at second">
353353
<failure type="testCodeFailure" message="thrown from subtest sync throw fails at second">
354-
Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at second
355-
* {
354+
[Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at second] {
356355
code: 'ERR_TEST_FAILURE',
357356
failureType: 'testCodeFailure',
358357
cause: Error: thrown from subtest sync throw fails at second
@@ -362,8 +361,6 @@ Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at second
362361
*
363362
*
364363
*
365-
*
366-
*
367364
}
368365
</failure>
369366
</testcase>

test/fixtures/test-runner/output/output.snapshot

-2
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,6 @@ not ok 51 - custom inspect symbol that throws fail
598598
*
599599
*
600600
*
601-
*
602-
*
603601
...
604602
1..2
605603
not ok 52 - subtest sync throw fails

test/fixtures/test-runner/output/output_cli.snapshot

-2
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,6 @@ not ok 51 - custom inspect symbol that throws fail
606606
*
607607
*
608608
*
609-
*
610-
*
611609
...
612610
1..2
613611
not ok 52 - subtest sync throw fails

test/fixtures/test-runner/output/spec_reporter.snapshot

-2
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,6 @@
285285
*
286286
*
287287
*
288-
*
289-
*
290288

291289
*
292290
timed out async test (*ms)

test/fixtures/test-runner/output/spec_reporter_cli.snapshot

-2
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,6 @@
288288
*
289289
*
290290
*
291-
*
292-
*
293291

294292
*
295293
timed out async test (*ms)

test/parallel/test-runner-module-mocking.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -449,13 +449,15 @@ test('mocks are automatically restored', async (t) => {
449449
assert.strictEqual(mocked.fn(), 43);
450450
});
451451

452-
const cjsMock = require(cjsFixture);
453-
const esmMock = await import(esmFixture);
452+
t.test('checks original behavior', async () => {
453+
const cjsMock = require(cjsFixture);
454+
const esmMock = await import(esmFixture);
454455

455-
assert.strictEqual(cjsMock.string, 'original cjs string');
456-
assert.strictEqual(cjsMock.fn, undefined);
457-
assert.strictEqual(esmMock.string, 'original esm string');
458-
assert.strictEqual(esmMock.fn, undefined);
456+
assert.strictEqual(cjsMock.string, 'original cjs string');
457+
assert.strictEqual(cjsMock.fn, undefined);
458+
assert.strictEqual(esmMock.string, 'original esm string');
459+
assert.strictEqual(esmMock.fn, undefined);
460+
});
459461
});
460462

461463
test('mocks can be restored independently', async (t) => {

0 commit comments

Comments
 (0)