@@ -148,6 +148,42 @@ test('skip() method with message', (t) => {
148148});
149149```
150150
151+ ### ` only ` tests
152+
153+ If Node.js is started with the [ ` --test-only ` ] [ ] command-line option, it is
154+ possible to skip all top level tests except for a selected subset by passing
155+ the ` only ` option to the tests that should be run. When a test with the ` only `
156+ option set is run, all subtests are also run. The test context's ` runOnly() `
157+ method can be used to implement the same behavior at the subtest level.
158+
159+ ``` js
160+ // Assume Node.js is run with the --test-only command-line option.
161+ // The 'only' option is set, so this test is run.
162+ test (' this test is run' , { only: true }, async (t ) => {
163+ // Within this test, all subtests are run by default.
164+ await t .test (' running subtest' );
165+
166+ // The test context can be updated to run subtests with the 'only' option.
167+ t .runOnly (true );
168+ await t .test (' this subtest is now skipped' );
169+ await t .test (' this subtest is run' , { only: true });
170+
171+ // Switch the context back to execute all tests.
172+ t .runOnly (false );
173+ await t .test (' this subtest is now run' );
174+
175+ // Explicitly do not run these tests.
176+ await t .test (' skipped subtest 3' , { only: false });
177+ await t .test (' skipped subtest 4' , { skip: true });
178+ });
179+
180+ // The 'only' option is not set, so this test is skipped.
181+ test (' this test is not run' , () => {
182+ // This code is not run.
183+ throw new Error (' fail' );
184+ });
185+ ```
186+
151187## Extraneous asynchronous activity
152188
153189Once a test function finishes executing, the TAP results are output as quickly
@@ -197,6 +233,9 @@ added: REPLACEME
197233 * ` concurrency ` {number} The number of tests that can be run at the same time.
198234 If unspecified, subtests inherit this value from their parent.
199235 ** Default:** ` 1 ` .
236+ * ` only ` {boolean} If truthy, and the test context is configured to run
237+ ` only ` tests, then this test will be run. Otherwise, the test is skipped.
238+ ** Default:** ` false ` .
200239 * ` skip ` {boolean|string} If truthy, the test is skipped. If a string is
201240 provided, that string is displayed in the test results as the reason for
202241 skipping the test. ** Default:** ` false ` .
@@ -257,6 +296,19 @@ This function is used to write TAP diagnostics to the output. Any diagnostic
257296information is included at the end of the test's results. This function does
258297not return a value.
259298
299+ ### ` context.runOnly(shouldRunOnlyTests) `
300+
301+ <!-- YAML
302+ added: REPLACEME
303+ -->
304+
305+ * ` shouldRunOnlyTests ` {boolean} Whether or not to run ` only ` tests.
306+
307+ If ` shouldRunOnlyTests ` is truthy, the test context will only run tests that
308+ have the ` only ` option set. Otherwise, all tests are run. If Node.js was not
309+ started with the [ ` --test-only ` ] [ ] command-line option, this function is a
310+ no-op.
311+
260312### ` context.skip([message]) `
261313
262314<!-- YAML
@@ -296,6 +348,9 @@ added: REPLACEME
296348 * ` concurrency ` {number} The number of tests that can be run at the same time.
297349 If unspecified, subtests inherit this value from their parent.
298350 ** Default:** ` 1 ` .
351+ * ` only ` {boolean} If truthy, and the test context is configured to run
352+ ` only ` tests, then this test will be run. Otherwise, the test is skipped.
353+ ** Default:** ` false ` .
299354 * ` skip ` {boolean|string} If truthy, the test is skipped. If a string is
300355 provided, that string is displayed in the test results as the reason for
301356 skipping the test. ** Default:** ` false ` .
@@ -312,5 +367,6 @@ This function is used to create subtests under the current test. This function
312367behaves in the same fashion as the top level [ ` test() ` ] [ ] function.
313368
314369[ TAP ] : https://testanything.org/
370+ [ `--test-only` ] : cli.md#--test-only
315371[ `TestContext` ] : #class-testcontext
316372[ `test()` ] : #testname-options-fn
0 commit comments