Skip to content

Commit

Permalink
doc: add code examples to node test runner
Browse files Browse the repository at this point in the history
PR-URL: #43359
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Akhil Marsonya <akhil.marsonya27@gmail.com>
Reviewed-By: Harshitha K P <harshitha014@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
2 people authored and targos committed Jul 12, 2022
1 parent 06c367e commit cbaf120
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@ This function is used to write TAP diagnostics to the output. Any diagnostic
information is included at the end of the test's results. This function does
not return a value.

```js
test('top level test', (t) => {
t.diagnostic('A diagnostic message');
});
```

### `context.runOnly(shouldRunOnlyTests)`

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

```js
test('top level test', (t) => {
// The test context can be set to run subtests with the 'only' option.
t.runOnly(true);
return Promise.all([
t.test('this subtest is now skipped'),
t.test('this subtest is run', { only: true }),
]);
});
```

### `context.skip([message])`

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

```js
test('top level test', (t) => {
// Make sure to return here as well if the test contains additional logic.
t.skip('this is skipped');
});
```

### `context.todo([message])`

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

```js
test('top level test', (t) => {
// This test is marked as `TODO`
t.todo('this is a todo');
});
```

### `context.test([name][, options][, fn])`

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

```js
test('top level test', async (t) => {
await t.test(
'This is a subtest',
{ only: false, skip: false, concurrency: 1, todo: false },
(t) => {
assert.ok('some relevant assertion here');
}
);
});
```

[TAP]: https://testanything.org/
[`--test-only`]: cli.md#--test-only
[`--test`]: cli.md#--test
Expand Down

0 comments on commit cbaf120

Please sign in to comment.