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
40 changes: 40 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -4078,6 +4078,46 @@ An instance of `TestContext` is passed to each test function in order to
interact with the test runner. However, the `TestContext` constructor is not
exposed as part of the API.

### TestContext lifecycle

A new `TestContext` instance is created for every test execution.

The context remains available throughout the execution of the test, including
its associated hooks.

Each subtest receives its own independent `TestContext` instance,
rather than sharing the parent's context.

### Example

```mjs
import assert from 'node:assert/strict';
import test from 'node:test';

test('parent', async (parentContext) => {
await parentContext.test('child', (childContext) => {
assert.notStrictEqual(parentContext, childContext);
});
});
```

In this example, the parent test and the subtest each receive their own
`TestContext` instance.

### Execution order

When hooks are registered on a test that contains subtests, they execute in
the following order for each subtest:

1. `before`
2. `beforeEach`
3. subtest execution
4. `afterEach`
5. `after`

When multiple hooks of the same type are registered, they execute in
registration order.

### `context.before([fn][, options])`

<!-- YAML
Expand Down
Loading