Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
CR
  • Loading branch information
MoLow committed Apr 1, 2024
commit 16073b602a7e291e452760514de3b1f130c61ca0
39 changes: 36 additions & 3 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,18 @@ const { describe, it } = require('node:test');
## `only` tests

If Node.js is started with the [`--test-only`][] command-line option, it is
possible to skip all top level tests except for a selected subset by passing
the `only` option to the tests that should be run. When a test with the `only`
option set is run, all subtests are also run. The test context's `runOnly()`
possible to skip all tests except for a selected subset by passing
the `only` option to the tests that should run. When a test with the `only`
option is set, all subtests are also run.
if a suite has the `only` option set, all tests within the suite are run,
Comment thread
MoLow marked this conversation as resolved.
Resolved
unless it has descendants with the `only` option set, in which case only those
tests are run.

When using [subtests][] without the `describe()` alias, it is required to mark
Comment thread
MoLow marked this conversation as resolved.
Resolved
all ancestor tests with the `only` option to run only a
selected subset of tests.

The test context's `runOnly()`
method can be used to implement the same behavior at the subtest level. Tests
that are not executed are omitted from the test runner output.

Expand Down Expand Up @@ -262,6 +271,29 @@ test('this test is not run', () => {
// This code is not run.
throw new Error('fail');
});

describe('a suite', () => {
// The 'only' option is set, so this test is run.
it('this test is run', { only: true }, () => {
Comment thread
MoLow marked this conversation as resolved.
Resolved
// This code is run.
});

it('this test is not run', () => {
// This code is not run.
throw new Error('fail');
});
});

describe.only('a suite', () => {
// The 'only' option is set, so this test is run.
Comment thread
MoLow marked this conversation as resolved.
Resolved
it('this test is run', () => {
// This code is run.
});

it('this test is run', () => {
// This code is run.
});
});
```

## Filtering tests by name
Expand Down Expand Up @@ -3145,6 +3177,7 @@ Can be used to abort test subtasks when the test has been aborted.
[describe options]: #describename-options-fn
[it options]: #testname-options-fn
[stream.compose]: stream.md#streamcomposestreams
[subtests]: #subtests
[suite options]: #suitename-options-fn
[test reporters]: #test-reporters
[test runner execution model]: #test-runner-execution-model
4 changes: 4 additions & 0 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ class Test extends AsyncResource {
this.filtered = true;
this.parent.filteredSubtestCount++;
}

if (testOnlyFlag && only === false) {
fn = noop;
}
}

switch (typeof concurrency) {
Expand Down
9 changes: 8 additions & 1 deletion test/fixtures/test-runner/output/only_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('describe only = undefined, with subtests', common.mustCall(() => {
test('async subtest should not run', common.mustNotCall());
}));

describe('describe only = false, with subtests', { only: false }, common.mustCall(() => {
describe('describe only = false, with subtests', { only: false }, common.mustNotCall(() => {
test('async subtest should not run', common.mustNotCall());
}));

Expand All @@ -112,3 +112,10 @@ describe.only('describe only = true, with nested subtests', common.mustCall(() =
test('nested test should run', common.mustCall());
}));
}));

describe('describe only = false, with nested only subtests', { only: false }, common.mustNotCall(() => {
test('async subtest should not run', common.mustNotCall());
describe('nested describe', common.mustNotCall(() => {
test.only('nested test should run', common.mustNotCall());
}));
}));