Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add code examples to node test runner #43359

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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