Skip to content

Commit

Permalink
test_runner: increase test planning features
Browse files Browse the repository at this point in the history
Co-authored-by: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
  • Loading branch information
marco-ippolito and VoltrexKeyva committed May 6, 2024
1 parent 01decff commit 607b2fb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
14 changes: 7 additions & 7 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,7 @@ changes:
* `timeout` {number} A number of milliseconds the test will fail after.
If unspecified, subtests inherit this value from their parent.
**Default:** `Infinity`.
* `plan` {number} The number of assertions expected to be run in the test.
* `plan` {number} The number of assertions and subtests expected to be run in the test.
If the number of assertions run in the test does not match the number
specified in the plan, the test will fail.
**Default:** `undefined`.
Expand Down Expand Up @@ -2976,19 +2976,19 @@ added:
- REPLACEME
-->

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

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

> Note: To make sure assertion are tracked, it must be used `t.assert` instead of `assert` directly.
> Note: To make sure assertions are tracked, `t.assert` must be used instead of `assert` directly.
```js
test('top level test', (t) => {
t.plan(2);
t.assert.ok('some relevant assertion here');
t.assert.ok('another relevant assertion here');
t.subtest('subtest', ()=> {});

Check failure on line 2991 in doc/api/test.md

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing space before =>
});
```

Expand Down Expand Up @@ -3122,7 +3122,7 @@ changes:
* `timeout` {number} A number of milliseconds the test will fail after.
If unspecified, subtests inherit this value from their parent.
**Default:** `Infinity`.
* `plan` {number} The number of assertions expected to be run in the test.
* `plan` {number} The number of assertions and subtests expected to be run in the test.
If the number of assertions run in the test does not match the number
specified in the plan, the test will fail.
**Default:** `undefined`.
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ class TestContext {
loc: getCallerLocation(),
};

const { plan } = this.#test;
if (plan !== null) {
plan.actual++;
}

const subtest = this.#test.createSubtest(
// eslint-disable-next-line no-use-before-define
Test, name, options, fn, overrides,
Expand Down
9 changes: 7 additions & 2 deletions test/fixtures/test-runner/output/test-runner-plan.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ test('less assertions than planned', (t) => {
test('more assertions than planned', (t) => {
t.plan(1);
t.assert.ok(true);
t.assert.ok(true);
t.assert.ok(true);
});

test('subtesting correctly', (t) => {
test('subtesting', (t) => {
t.plan(1);
t.test('subtest', () => {});
});

test('subtesting correctly', (t) => {
t.plan(2);
t.assert.ok(true);
t.test('subtest', (st) => {
st.plan(1);
Expand Down
33 changes: 20 additions & 13 deletions test/fixtures/test-runner/output/test-runner-plan.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,25 @@ not ok 3 - more assertions than planned
error: 'plan expected 1 assertions but received 2'
code: 'ERR_TEST_FAILURE'
...
# Subtest: subtesting
# Subtest: subtest
ok 1 - subtest
---
duration_ms: *
...
1..1
ok 4 - subtesting
---
duration_ms: *
...
# Subtest: subtesting correctly
# Subtest: subtest
ok 1 - subtest
---
duration_ms: *
...
1..1
ok 4 - subtesting correctly
ok 5 - subtesting correctly
---
duration_ms: *
...
Expand All @@ -40,16 +51,12 @@ ok 4 - subtesting correctly
duration_ms: *
...
1..1
not ok 5 - correctly ignoring subtesting plan
ok 6 - correctly ignoring subtesting plan
---
duration_ms: *
location: '/test/fixtures/test-runner/output/test-runner-plan.js:(LINE):1'
failureType: 'testCodeFailure'
error: 'plan expected 1 assertions but received 0'
code: 'ERR_TEST_FAILURE'
...
# Subtest: failing planning by options
not ok 6 - failing planning by options
not ok 7 - failing planning by options
---
duration_ms: *
location: '/test/fixtures/test-runner/output/test-runner-plan.js:(LINE):1'
Expand All @@ -58,7 +65,7 @@ not ok 6 - failing planning by options
code: 'ERR_TEST_FAILURE'
...
# Subtest: not failing planning by options
ok 7 - not failing planning by options
ok 8 - not failing planning by options
---
duration_ms: *
...
Expand All @@ -69,15 +76,15 @@ ok 7 - not failing planning by options
duration_ms: *
...
1..1
ok 8 - subtest planning by options
ok 9 - subtest planning by options
---
duration_ms: *
...
1..8
# tests 11
1..9
# tests 13
# suites 0
# pass 7
# fail 4
# pass 10
# fail 3
# cancelled 0
# skipped 0
# todo 0
Expand Down

0 comments on commit 607b2fb

Please sign in to comment.