Skip to content

Commit 607b2fb

Browse files
test_runner: increase test planning features
Co-authored-by: Mohammed Keyvanzadeh <[email protected]>
1 parent 01decff commit 607b2fb

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

doc/api/test.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,7 @@ changes:
13641364
* `timeout` {number} A number of milliseconds the test will fail after.
13651365
If unspecified, subtests inherit this value from their parent.
13661366
**Default:** `Infinity`.
1367-
* `plan` {number} The number of assertions expected to be run in the test.
1367+
* `plan` {number} The number of assertions and subtests expected to be run in the test.
13681368
If the number of assertions run in the test does not match the number
13691369
specified in the plan, the test will fail.
13701370
**Default:** `undefined`.
@@ -2976,19 +2976,19 @@ added:
29762976
- REPLACEME
29772977
-->
29782978

2979-
* `count` {number} The number of assertions that are expected to run.
2979+
* `count` {number} The number of assertions and subtests that are expected to run.
29802980

2981-
This function is used to set the number of assertions that are expected to run
2982-
within the test. If the number of assertions that run does not match the
2981+
This function is used to set the number of assertions and subtests that are expected to run
2982+
within the test. If the number of assertions and subtests that run does not match the
29832983
expected count, the test will fail.
29842984

2985-
> Note: To make sure assertion are tracked, it must be used `t.assert` instead of `assert` directly.
2985+
> Note: To make sure assertions are tracked, `t.assert` must be used instead of `assert` directly.
29862986
29872987
```js
29882988
test('top level test', (t) => {
29892989
t.plan(2);
29902990
t.assert.ok('some relevant assertion here');
2991-
t.assert.ok('another relevant assertion here');
2991+
t.subtest('subtest', ()=> {});
29922992
});
29932993
```
29942994

@@ -3122,7 +3122,7 @@ changes:
31223122
* `timeout` {number} A number of milliseconds the test will fail after.
31233123
If unspecified, subtests inherit this value from their parent.
31243124
**Default:** `Infinity`.
3125-
* `plan` {number} The number of assertions expected to be run in the test.
3125+
* `plan` {number} The number of assertions and subtests expected to be run in the test.
31263126
If the number of assertions run in the test does not match the number
31273127
specified in the plan, the test will fail.
31283128
**Default:** `undefined`.

lib/internal/test_runner/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ class TestContext {
266266
loc: getCallerLocation(),
267267
};
268268

269+
const { plan } = this.#test;
270+
if (plan !== null) {
271+
plan.actual++;
272+
}
273+
269274
const subtest = this.#test.createSubtest(
270275
// eslint-disable-next-line no-use-before-define
271276
Test, name, options, fn, overrides,

test/fixtures/test-runner/output/test-runner-plan.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ test('less assertions than planned', (t) => {
1414
test('more assertions than planned', (t) => {
1515
t.plan(1);
1616
t.assert.ok(true);
17-
t.assert.ok(true);
17+
t.assert.ok(true);
1818
});
1919

20-
test('subtesting correctly', (t) => {
20+
test('subtesting', (t) => {
2121
t.plan(1);
22+
t.test('subtest', () => {});
23+
});
24+
25+
test('subtesting correctly', (t) => {
26+
t.plan(2);
2227
t.assert.ok(true);
2328
t.test('subtest', (st) => {
2429
st.plan(1);

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,25 @@ not ok 3 - more assertions than planned
2222
error: 'plan expected 1 assertions but received 2'
2323
code: 'ERR_TEST_FAILURE'
2424
...
25+
# Subtest: subtesting
26+
# Subtest: subtest
27+
ok 1 - subtest
28+
---
29+
duration_ms: *
30+
...
31+
1..1
32+
ok 4 - subtesting
33+
---
34+
duration_ms: *
35+
...
2536
# Subtest: subtesting correctly
2637
# Subtest: subtest
2738
ok 1 - subtest
2839
---
2940
duration_ms: *
3041
...
3142
1..1
32-
ok 4 - subtesting correctly
43+
ok 5 - subtesting correctly
3344
---
3445
duration_ms: *
3546
...
@@ -40,16 +51,12 @@ ok 4 - subtesting correctly
4051
duration_ms: *
4152
...
4253
1..1
43-
not ok 5 - correctly ignoring subtesting plan
54+
ok 6 - correctly ignoring subtesting plan
4455
---
4556
duration_ms: *
46-
location: '/test/fixtures/test-runner/output/test-runner-plan.js:(LINE):1'
47-
failureType: 'testCodeFailure'
48-
error: 'plan expected 1 assertions but received 0'
49-
code: 'ERR_TEST_FAILURE'
5057
...
5158
# Subtest: failing planning by options
52-
not ok 6 - failing planning by options
59+
not ok 7 - failing planning by options
5360
---
5461
duration_ms: *
5562
location: '/test/fixtures/test-runner/output/test-runner-plan.js:(LINE):1'
@@ -58,7 +65,7 @@ not ok 6 - failing planning by options
5865
code: 'ERR_TEST_FAILURE'
5966
...
6067
# Subtest: not failing planning by options
61-
ok 7 - not failing planning by options
68+
ok 8 - not failing planning by options
6269
---
6370
duration_ms: *
6471
...
@@ -69,15 +76,15 @@ ok 7 - not failing planning by options
6976
duration_ms: *
7077
...
7178
1..1
72-
ok 8 - subtest planning by options
79+
ok 9 - subtest planning by options
7380
---
7481
duration_ms: *
7582
...
76-
1..8
77-
# tests 11
83+
1..9
84+
# tests 13
7885
# suites 0
79-
# pass 7
80-
# fail 4
86+
# pass 10
87+
# fail 3
8188
# cancelled 0
8289
# skipped 0
8390
# todo 0

0 commit comments

Comments
 (0)