doc: add code examples to node test runner

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Wassim Chegham 2022-06-09 12:23:40 +02:00 committed by Luigi Pinca
parent 72f9597d3e
commit 6975dd1425

View File

@ -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 information is included at the end of the test's results. This function does
not return a value. not return a value.
```js
test('top level test', (t) => {
t.diagnostic('A diagnostic message');
});
```
### `context.runOnly(shouldRunOnlyTests)` ### `context.runOnly(shouldRunOnlyTests)`
<!-- YAML <!-- YAML
@ -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 started with the [`--test-only`][] command-line option, this function is a
no-op. 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])` ### `context.skip([message])`
<!-- YAML <!-- YAML
@ -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 not terminate execution of the test function. This function does not return a
value. 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])` ### `context.todo([message])`
<!-- YAML <!-- YAML
@ -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 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. 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])` ### `context.test([name][, options][, fn])`
<!-- YAML <!-- YAML
@ -427,6 +458,18 @@ added: v18.0.0
This function is used to create subtests under the current test. This function This function is used to create subtests under the current test. This function
behaves in the same fashion as the top level [`test()`][] 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/ [TAP]: https://testanything.org/
[`--test-only`]: cli.md#--test-only [`--test-only`]: cli.md#--test-only
[`--test`]: cli.md#--test [`--test`]: cli.md#--test