Skip to content

Commit cbaf120

Browse files
manekinekkoaduh95
authored andcommitted
doc: add code examples to node test runner
PR-URL: #43359 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Akhil Marsonya <[email protected]> Reviewed-By: Harshitha K P <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: Rich Trott <[email protected]> Co-authored-by: Antoine du Hamel <[email protected]>
1 parent 06c367e commit cbaf120

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

doc/api/test.md

+43
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ This function is used to write TAP diagnostics to the output. Any diagnostic
357357
information is included at the end of the test's results. This function does
358358
not return a value.
359359

360+
```js
361+
test('top level test', (t) => {
362+
t.diagnostic('A diagnostic message');
363+
});
364+
```
365+
360366
### `context.runOnly(shouldRunOnlyTests)`
361367

362368
<!-- YAML
@@ -370,6 +376,17 @@ have the `only` option set. Otherwise, all tests are run. If Node.js was not
370376
started with the [`--test-only`][] command-line option, this function is a
371377
no-op.
372378

379+
```js
380+
test('top level test', (t) => {
381+
// The test context can be set to run subtests with the 'only' option.
382+
t.runOnly(true);
383+
return Promise.all([
384+
t.test('this subtest is now skipped'),
385+
t.test('this subtest is run', { only: true }),
386+
]);
387+
});
388+
```
389+
373390
### `context.skip([message])`
374391

375392
<!-- YAML
@@ -383,6 +400,13 @@ This function causes the test's output to indicate the test as skipped. If
383400
not terminate execution of the test function. This function does not return a
384401
value.
385402

403+
```js
404+
test('top level test', (t) => {
405+
// Make sure to return here as well if the test contains additional logic.
406+
t.skip('this is skipped');
407+
});
408+
```
409+
386410
### `context.todo([message])`
387411

388412
<!-- YAML
@@ -395,6 +419,13 @@ This function adds a `TODO` directive to the test's output. If `message` is
395419
provided, it is included in the TAP output. Calling `todo()` does not terminate
396420
execution of the test function. This function does not return a value.
397421

422+
```js
423+
test('top level test', (t) => {
424+
// This test is marked as `TODO`
425+
t.todo('this is a todo');
426+
});
427+
```
428+
398429
### `context.test([name][, options][, fn])`
399430

400431
<!-- YAML
@@ -427,6 +458,18 @@ added: v18.0.0
427458
This function is used to create subtests under the current test. This function
428459
behaves in the same fashion as the top level [`test()`][] function.
429460

461+
```js
462+
test('top level test', async (t) => {
463+
await t.test(
464+
'This is a subtest',
465+
{ only: false, skip: false, concurrency: 1, todo: false },
466+
(t) => {
467+
assert.ok('some relevant assertion here');
468+
}
469+
);
470+
});
471+
```
472+
430473
[TAP]: https://testanything.org/
431474
[`--test-only`]: cli.md#--test-only
432475
[`--test`]: cli.md#--test

0 commit comments

Comments
 (0)