Skip to content
Open
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
79 changes: 79 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,85 @@ Any subtests that are still outstanding when their parent finishes
are cancelled and treated as failures. Any subtest failures cause the parent
test to fail.

## Suites

Suites provide a way to group related tests under a common name. A suite
is created with the `suite()` function (or its alias `describe()`). The
callback passed to `suite()` is executed synchronously and is used only
to register nested tests or nested suites.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
to register nested tests or nested suites.
to register lifecycle methods (such as `before()` and `after()`), nested tests, or nested suites.


A suite reports a result, but its status is fully determined by the
tests and suites inside it --- suites cannot fail independently.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tests and suites inside it --- suites cannot fail independently.
tests and suites inside it - suites cannot fail independently.


A suite always waits for all of its nested tests and nested suites to
finish before completing. The following example demonstrates a
suite with nested tests and other suites.

``` js
import { suite, test } from 'node:test';
import assert from 'node:assert';
suite('math operations', () => {
test('addition', () => {
assert.strictEqual(1 + 1, 2);
});
test('multiplication', () => {
assert.strictEqual(2 * 2, 4);
});
});
```

``` js
suite('user features', () => {
suite('profile', () => {
test('updates display name', () => {
assert.ok(true);
});
});
suite('settings', () => {
test('enables notifications', () => {
assert.ok(true);
});
});
});
```
Comment on lines +142 to +168
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets simplify this example to demonstrate everything in one code block.

Suggested change
``` js
import { suite, test } from 'node:test';
import assert from 'node:assert';
suite('math operations', () => {
test('addition', () => {
assert.strictEqual(1 + 1, 2);
});
test('multiplication', () => {
assert.strictEqual(2 * 2, 4);
});
});
```
``` js
suite('user features', () => {
suite('profile', () => {
test('updates display name', () => {
assert.ok(true);
});
});
suite('settings', () => {
test('enables notifications', () => {
assert.ok(true);
});
});
});
```
```js
import { suite, test } from 'node:test';
import assert from 'node:assert';
suite('math operations', () => {
test('addition', () => {
assert.strictEqual(1 + 1, 2);
});
suite('multiplication', () => {
test('negative one', () => {
assert.strictEqual(2 * -1, -2);
});
test('zero', () => {
assert.strictEqual(2 * 0, 0);
});
});
});
```


**Suites vs. Subtests**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**Suites vs. Subtests**
### Suites vs. Subtests


Both suites and subtests structure test hierarchies, but they behave
differently in important ways.

*Execution behavior*
| Feature | Suite (`suite()`) | Subtest (`t.test()`) |
|-------------------------------------------|---------------------------------------------------------------------------------------------|----------------------------------------------|
| Parent waits for children | Always | Only if awaited |
| Pending children canceled if parent ends | No, unless the suite is canceled via `AbortSignal` | Yes |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is another way to cancel the parent suite and the subtests continue to execute?

| Callback may be async | Suite callbacks run synchronously; returning a Promise does not affect execution | Yes (async functions are allowed) |
| Callback controls execution order | No — tests execute strictly in declaration order; the suite callback cannot pause execution | Yes — execution order controlled via `await` |


*Suite example*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*Suite example*
### Suite example


``` js
suite('group', () => {
test('test 1', () => assert.ok(true));
test('test 2', () => assert.ok(true));
});
```

The suite finishes only after both tests complete.

*Subtest example*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*Subtest example*
### Subtest example


``` js
test('parent test', async (t) => {
await t.test('subtest 1', () => assert.ok(true));
await t.test('subtest 2', () => assert.ok(true));
});
```

If the `await` keywords were omitted, the parent test would complete
early and the test runner would cancel the unfinished subtests.

## Skipping tests

Individual tests can be skipped by passing the `skip` option to the test, or by
Expand Down