@@ -148,6 +148,42 @@ test('skip() method with message', (t) => {
148
148
});
149
149
```
150
150
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
+
151
187
## Extraneous asynchronous activity
152
188
153
189
Once a test function finishes executing, the TAP results are output as quickly
@@ -197,6 +233,9 @@ added: REPLACEME
197
233
* ` concurrency ` {number} The number of tests that can be run at the same time.
198
234
If unspecified, subtests inherit this value from their parent.
199
235
** 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 ` .
200
239
* ` skip ` {boolean|string} If truthy, the test is skipped. If a string is
201
240
provided, that string is displayed in the test results as the reason for
202
241
skipping the test. ** Default:** ` false ` .
@@ -257,6 +296,19 @@ This function is used to write TAP diagnostics to the output. Any diagnostic
257
296
information is included at the end of the test's results. This function does
258
297
not return a value.
259
298
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
+
260
312
### ` context.skip([message]) `
261
313
262
314
<!-- YAML
@@ -296,6 +348,9 @@ added: REPLACEME
296
348
* ` concurrency ` {number} The number of tests that can be run at the same time.
297
349
If unspecified, subtests inherit this value from their parent.
298
350
** 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 ` .
299
354
* ` skip ` {boolean|string} If truthy, the test is skipped. If a string is
300
355
provided, that string is displayed in the test results as the reason for
301
356
skipping the test. ** Default:** ` false ` .
@@ -312,5 +367,6 @@ This function is used to create subtests under the current test. This function
312
367
behaves in the same fashion as the top level [ ` test() ` ] [ ] function.
313
368
314
369
[ TAP ] : https://testanything.org/
370
+ [ `--test-only` ] : cli.md#--test-only
315
371
[ `TestContext` ] : #class-testcontext
316
372
[ `test()` ] : #testname-options-fn
0 commit comments