Use node --test with --test-name-pattern and an explicit test-file path. For an ordinary nested t.test() case, the filter must allow its enclosing test to run too. Node’s documentation warns that matching a child alone is insufficient if its parent does not match. Node.js filtering documentation
For the example below, select the accepts integers case and its parent with:
node --test --test-name-pattern="^number parsing$" --test-name-pattern="accepts integers$" test/parser.test.mjs
The first pattern matches the parent exactly. The second matches the desired child’s name suffix. Neither matches the sibling case in this example.
Start with a parent and two distinguishable cases
Save this as test/parser.test.mjs:
import test from 'node:test';
import assert from 'node:assert/strict';
test('number parsing', async (t) => {
await t.test('accepts integers', () => {
assert.equal(Number('42'), 42);
});
await t.test('returns NaN for text', () => {
assert.ok(Number.isNaN(Number('hello')));
});
});
The enclosing callback creates the two subtests, with each subtest call awaited, following the structure of Node’s documented nested-test example. Node.js filtering documentation
Select the parent and only the intended child
Name patterns are JavaScript regular expressions. Repeated --test-name-pattern options provide alternative matches: a test name does not have to match every expression. Node’s documentation demonstrates using separate pattern options to select a parent and its children. Node.js filtering documentation
For this file:
^number parsing$matches the enclosing test exactly.accepts integers$matches the desired child-name suffix, including when preceded by an ancestor name.returns NaN for textmatches neither expression.
You can express the same selection with regex alternation:
node --test --test-name-pattern="^number parsing$|accepts integers$" test/parser.test.mjs
The parent expression’s anchors are deliberate. A bare number parsing expression can also match that text inside a longer hierarchical name; it is not an exact parent-only match.
For deeper ordinary t.test() nesting, allow every enclosing test to run, not just the immediate parent.
Distinguish repeated case names in suites
A child-name suffix is not necessarily unique across a file. When different branches reuse a case name, use the hierarchy to distinguish them.
Node’s documentation demonstrates this with describe()/it() suites: prefix the case name with all its ancestor names, separated by spaces. For example, if suites named decimal and hexadecimal both contain it('parses input', ...), select the decimal case with:
node --test --test-name-pattern="^decimal parses input$" test/parser-suites.test.mjs
This applies the documented full-name selection technique, with anchors added for an exact match. It is a suite example—not a reason to omit the enclosing ordinary test from the earlier t.test() command. Node.js filtering documentation
Know what a focused rerun still executes
A name filter is not complete execution isolation:
- Specify the file explicitly. Name patterns do not change which files the runner executes.
- Expect the enclosing ordinary test to execute. In the
t.test()example, its callback creates the selected child. - Expect applicable hooks to run. Node runs corresponding hooks, such as
beforeEach(), for executed tests. - Check skip filters. If both
--test-name-patternand--test-skip-patternare supplied, a test must match the inclusion filter and avoid exclusion by the skip filter.
These behaviors follow the filtering reference.
The CLI reference records --test-name-pattern as added in Node.js v18.11.0. That is the option’s introduction version, not a guarantee that every current nested-filter example behaves identically on older releases. The commands here follow the current documentation; consult your runtime’s version-specific reference when working with an older version. Node.js CLI reference
Sources are linked throughout this guide. Product capabilities can change; consult the linked documentation for your deployment.
Read our editorial approach ↗