Skip to content
Closed
Show file tree
Hide file tree
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: 42 additions & 1 deletion doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -3875,7 +3875,9 @@ added: v25.0.0

* Type: {number}

Number of times the test has been attempted.
The attempt number of the test. This value is zero-based, so the first attempt is `0`,
the second attempt is `1`, and so on. This property is useful in conjunction with the
`--test-rerun-failures` option to determine which attempt the test is currently running.

### `context.workerId`

Expand Down Expand Up @@ -4230,6 +4232,45 @@ added:

Can be used to abort test subtasks when the test has been aborted.

### `context.passed`

<!-- YAML
added: REPLACEME
-->

* Type: {boolean}

Indicates whether the suite and all of its subtests have passed.

### `context.attempt`

<!-- YAML
added: REPLACEME
-->

* Type: {number}

The attempt number of the suite. This value is zero-based, so the first attempt is `0`,
the second attempt is `1`, and so on. This property is useful in conjunction with the
`--test-rerun-failures` option to determine the attempt number of the current run.

### `context.diagnostic(message)`

<!-- YAML
added: REPLACEME
-->

* `message` {string} A diagnostic message to output.

Output a diagnostic message. This is typically used for logging information
about the current suite or its tests.

```js
test.describe('my suite', (suite) => {
suite.diagnostic('Suite diagnostic message');
});
```

[TAP]: https://testanything.org/
[`--experimental-test-coverage`]: cli.md#--experimental-test-coverage
[`--experimental-test-module-mocks`]: cli.md#--experimental-test-module-mocks
Expand Down
12 changes: 12 additions & 0 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,18 @@ class SuiteContext {
get fullName() {
return getFullName(this.#suite);
}

get passed() {
return this.#suite.passed;
}

get attempt() {
return this.#suite.attempt ?? 0;
}

diagnostic(message) {
this.#suite.diagnostic(message);
}
}

function parseExpectFailure(expectFailure) {
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-runner-test-fullname.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ before(common.mustCall((t) => {

suite('suite', common.mustCall((t) => {
assert.strictEqual(t.fullName, 'suite');
// Test new SuiteContext properties
assert.strictEqual(typeof t.passed, 'boolean');
assert.strictEqual(t.attempt, 0);
// diagnostic() can be called to add diagnostic output
t.diagnostic('Suite diagnostic message');

test('test', (t) => {
assert.strictEqual(t.fullName, 'suite > test');
Expand All @@ -26,3 +31,18 @@ suite('suite', common.mustCall((t) => {
test((t) => {
assert.strictEqual(t.fullName, '<anonymous>');
});

// Test SuiteContext passed, attempt, and diagnostic properties
suite('suite with context checks', common.mustCall((ctx) => {
assert.strictEqual(ctx.fullName, 'suite with context checks');
assert.strictEqual(typeof ctx.passed, 'boolean');
assert.strictEqual(ctx.attempt, 0);
// Verify diagnostic method is callable
ctx.diagnostic('Test diagnostic message in suite');

test('child test', () => {
// Verify properties are accessible in nested test
assert.strictEqual(typeof ctx.passed, 'boolean');
assert.strictEqual(ctx.attempt, 0);
});
}));
Loading