Skip to content

Commit 65699a2

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 5dca44d commit 65699a2

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
@@ -446,6 +446,12 @@ This function is used to write TAP diagnostics to the output. Any diagnostic
446446
information is included at the end of the test's results. This function does
447447
not return a value.
448448

449+
```js
450+
test('top level test', (t) => {
451+
t.diagnostic('A diagnostic message');
452+
});
453+
```
454+
449455
### `context.runOnly(shouldRunOnlyTests)`
450456

451457
<!-- YAML
@@ -459,6 +465,17 @@ have the `only` option set. Otherwise, all tests are run. If Node.js was not
459465
started with the [`--test-only`][] command-line option, this function is a
460466
no-op.
461467

468+
```js
469+
test('top level test', (t) => {
470+
// The test context can be set to run subtests with the 'only' option.
471+
t.runOnly(true);
472+
return Promise.all([
473+
t.test('this subtest is now skipped'),
474+
t.test('this subtest is run', { only: true }),
475+
]);
476+
});
477+
```
478+
462479
### `context.skip([message])`
463480

464481
<!-- YAML
@@ -472,6 +489,13 @@ This function causes the test's output to indicate the test as skipped. If
472489
not terminate execution of the test function. This function does not return a
473490
value.
474491

492+
```js
493+
test('top level test', (t) => {
494+
// Make sure to return here as well if the test contains additional logic.
495+
t.skip('this is skipped');
496+
});
497+
```
498+
475499
### `context.todo([message])`
476500

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

511+
```js
512+
test('top level test', (t) => {
513+
// This test is marked as `TODO`
514+
t.todo('this is a todo');
515+
});
516+
```
517+
487518
### `context.test([name][, options][, fn])`
488519

489520
<!-- YAML
@@ -516,6 +547,18 @@ added: REPLACEME
516547
This function is used to create subtests under the current test. This function
517548
behaves in the same fashion as the top level [`test()`][] function.
518549

550+
```js
551+
test('top level test', async (t) => {
552+
await t.test(
553+
'This is a subtest',
554+
{ only: false, skip: false, concurrency: 1, todo: false },
555+
(t) => {
556+
assert.ok('some relevant assertion here');
557+
}
558+
);
559+
});
560+
```
561+
519562
[TAP]: https://testanything.org/
520563
[`--test-only`]: cli.md#--test-only
521564
[`--test`]: cli.md#--test

0 commit comments

Comments
 (0)