Skip to content

Commit ae25faf

Browse files
committed
Revert "test_runner: remove promises returned by t.test()"
This reverts commit 1a2eb15.
1 parent f812955 commit ae25faf

File tree

11 files changed

+67
-36
lines changed

11 files changed

+67
-36
lines changed

doc/api/test.md

Lines changed: 36 additions & 26 deletions
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-
t.test('subtest 1', (t) => {
108+
await t.test('subtest 1', (t) => {
109109
assert.strictEqual(1, 1);
110110
});
111111

112-
t.test('subtest 2', (t) => {
112+
await t.test('subtest 2', (t) => {
113113
assert.strictEqual(2, 2);
114114
});
115115
});
@@ -118,7 +118,12 @@ test('top level test', async (t) => {
118118
> **Note:** `beforeEach` and `afterEach` hooks are triggered
119119
> between each subtest execution.
120120
121-
Any subtest failures cause the parent test to fail.
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.
122127

123128
## Skipping tests
124129

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

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

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

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

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

305310
```js
306311
test('test 1', async (t) => {
307-
t.test('test 2');
308-
t.test('test 3');
312+
await t.test('test 2');
313+
await t.test('test 3');
309314
});
310315

311316
test('Test 4', async (t) => {
312-
t.test('Test 5');
313-
t.test('test 6');
317+
await t.test('Test 5');
318+
await t.test('test 6');
314319
});
315320
```
316321

@@ -3245,9 +3250,12 @@ before each subtest of the current test.
32453250
```js
32463251
test('top level test', async (t) => {
32473252
t.beforeEach((t) => t.diagnostic(`about to run ${t.name}`));
3248-
t.test('This is a subtest', (t) => {
3249-
assert.ok('some relevant assertion here');
3250-
});
3253+
await t.test(
3254+
'This is a subtest',
3255+
(t) => {
3256+
assert.ok('some relevant assertion here');
3257+
},
3258+
);
32513259
});
32523260
```
32533261

@@ -3305,9 +3313,12 @@ after each subtest of the current test.
33053313
```js
33063314
test('top level test', async (t) => {
33073315
t.afterEach((t) => t.diagnostic(`finished running ${t.name}`));
3308-
t.test('This is a subtest', (t) => {
3309-
assert.ok('some relevant assertion here');
3310-
});
3316+
await t.test(
3317+
'This is a subtest',
3318+
(t) => {
3319+
assert.ok('some relevant assertion here');
3320+
},
3321+
);
33113322
});
33123323
```
33133324

@@ -3559,8 +3570,10 @@ no-op.
35593570
test('top level test', (t) => {
35603571
// The test context can be set to run subtests with the 'only' option.
35613572
t.runOnly(true);
3562-
t.test('this subtest is now skipped');
3563-
t.test('this subtest is run', { only: true });
3573+
return Promise.all([
3574+
t.test('this subtest is now skipped'),
3575+
t.test('this subtest is run', { only: true }),
3576+
]);
35643577
});
35653578
```
35663579

@@ -3632,10 +3645,6 @@ added:
36323645
- v18.0.0
36333646
- v16.17.0
36343647
changes:
3635-
- version:
3636-
- v24.0.0
3637-
pr-url: https://github.com/nodejs/node/pull/56664
3638-
description: This function no longer returns a `Promise`.
36393648
- version:
36403649
- v18.8.0
36413650
- v16.18.0
@@ -3680,13 +3689,14 @@ changes:
36803689
to this function is a [`TestContext`][] object. If the test uses callbacks,
36813690
the callback function is passed as the second argument. **Default:** A no-op
36823691
function.
3692+
* Returns: {Promise} Fulfilled with `undefined` once the test completes.
36833693

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

36873697
```js
36883698
test('top level test', async (t) => {
3689-
t.test(
3699+
await t.test(
36903700
'This is a subtest',
36913701
{ only: false, skip: false, concurrency: 1, todo: false, plan: 1 },
36923702
(t) => {

lib/internal/test_runner/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ class TestContext {
370370
Test, name, options, fn, overrides,
371371
);
372372

373-
subtest.start();
373+
return subtest.start();
374374
}
375375

376376
before(fn, options) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ Failed tests:
158158
*
159159
*
160160
*
161+
*
162+
*
161163
✖ subtest sync throw fails (*ms)
162164
'2 subtests failed'
163165
✖ timed out async test (*ms)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ not ok 16 - t.after throws - no subtests
576576
*
577577
*
578578
*
579+
*
579580
...
580581
1..2
581582
not ok 17 - t.beforeEach throws
@@ -606,6 +607,8 @@ not ok 17 - t.beforeEach throws
606607
*
607608
*
608609
*
610+
*
611+
*
609612
...
610613
# Subtest: 2
611614
not ok 2 - 2
@@ -626,6 +629,7 @@ not ok 17 - t.beforeEach throws
626629
*
627630
*
628631
*
632+
*
629633
...
630634
1..2
631635
not ok 18 - t.afterEach throws
@@ -753,6 +757,7 @@ not ok 21 - afterEach context when test fails
753757
*
754758
*
755759
*
760+
*
756761
...
757762
1..2
758763
not ok 22 - afterEach throws and test fails

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@
363363
*
364364
*
365365
*
366+
*
366367

367368
*
368369
1 (*ms)
@@ -375,6 +376,8 @@
375376
*
376377
*
377378
*
379+
*
380+
*
378381

379382
*
380383
2 (*ms)
@@ -388,6 +391,7 @@
388391
*
389392
*
390393
*
394+
*
391395

392396
*
393397
1 (*ms)
@@ -435,6 +439,7 @@
435439
*
436440
*
437441
*
442+
*
438443

439444
*
440445
t.after() is called if test body throws (*ms)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at first
355355
</testcase>
356356
<testcase name="sync throw fails at second" time="*" classname="test" failure="thrown from subtest sync throw fails at second">
357357
<failure type="testCodeFailure" message="thrown from subtest sync throw fails at second">
358-
[Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at second] {
358+
Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at second
359+
* {
359360
code: 'ERR_TEST_FAILURE',
360361
failureType: 'testCodeFailure',
361362
cause: Error: thrown from subtest sync throw fails at second
@@ -365,6 +366,8 @@ Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at first
365366
*
366367
*
367368
*
369+
*
370+
*
368371
}
369372
</failure>
370373
</testcase>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,8 @@ not ok 51 - custom inspect symbol that throws fail
606606
*
607607
*
608608
*
609+
*
610+
*
609611
...
610612
1..2
611613
not ok 52 - subtest sync throw fails

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@ not ok 51 - custom inspect symbol that throws fail
614614
*
615615
*
616616
*
617+
*
618+
*
617619
...
618620
1..2
619621
not ok 52 - subtest sync throw fails

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@
289289
*
290290
*
291291
*
292+
*
293+
*
292294

293295
*
294296
timed out async test (*ms)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@
292292
*
293293
*
294294
*
295+
*
296+
*
295297

296298
*
297299
timed out async test (*ms)

0 commit comments

Comments
 (0)