Skip to content

Commit fa2d6c7

Browse files
committed
test_runner: add passed, attempt, and diagnostic to SuiteContext
1 parent d9645d7 commit fa2d6c7

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

doc/api/test.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4230,6 +4230,45 @@ added:
42304230

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

4233+
### `context.passed`
4234+
4235+
<!-- YAML
4236+
added: REPLACEME
4237+
-->
4238+
4239+
* Type: {boolean}
4240+
4241+
Indicates whether the suite and all of its subtests have passed.
4242+
4243+
### `context.attempt`
4244+
4245+
<!-- YAML
4246+
added: REPLACEME
4247+
-->
4248+
4249+
* Type: {number}
4250+
4251+
The current attempt number of the suite. Used in conjunction with the
4252+
`--test-rerun-failures` option to determine the attempt number of the current
4253+
run.
4254+
4255+
### `context.diagnostic(message)`
4256+
4257+
<!-- YAML
4258+
added: REPLACEME
4259+
-->
4260+
4261+
* `message` {string} A diagnostic message to output.
4262+
4263+
Output a diagnostic message. This is typically used for logging information
4264+
about the current suite or its tests.
4265+
4266+
```js
4267+
test.describe('my suite', (suite) => {
4268+
suite.diagnostic('Suite diagnostic message');
4269+
});
4270+
```
4271+
42334272
[TAP]: https://testanything.org/
42344273
[`--experimental-test-coverage`]: cli.md#--experimental-test-coverage
42354274
[`--experimental-test-module-mocks`]: cli.md#--experimental-test-module-mocks

lib/internal/test_runner/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,18 @@ class SuiteContext {
494494
get fullName() {
495495
return getFullName(this.#suite);
496496
}
497+
498+
get passed() {
499+
return this.#suite.passed;
500+
}
501+
502+
get attempt() {
503+
return this.#suite.attempt ?? 0;
504+
}
505+
506+
diagnostic(message) {
507+
this.#suite.diagnostic(message);
508+
}
497509
}
498510

499511
function parseExpectFailure(expectFailure) {

test/parallel/test-runner-test-fullname.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ before(common.mustCall((t) => {
99

1010
suite('suite', common.mustCall((t) => {
1111
assert.strictEqual(t.fullName, 'suite');
12+
// Test new SuiteContext properties
13+
assert.strictEqual(typeof t.passed, 'boolean');
14+
assert.strictEqual(t.attempt, 0);
15+
// diagnostic() can be called to add diagnostic output
16+
t.diagnostic('Suite diagnostic message');
1217

1318
test('test', (t) => {
1419
assert.strictEqual(t.fullName, 'suite > test');
@@ -26,3 +31,18 @@ suite('suite', common.mustCall((t) => {
2631
test((t) => {
2732
assert.strictEqual(t.fullName, '<anonymous>');
2833
});
34+
35+
// Test SuiteContext passed, attempt, and diagnostic properties
36+
suite('suite with context checks', common.mustCall((ctx) => {
37+
assert.strictEqual(ctx.fullName, 'suite with context checks');
38+
assert.strictEqual(typeof ctx.passed, 'boolean');
39+
assert.strictEqual(ctx.attempt, 0);
40+
// Verify diagnostic method is callable
41+
ctx.diagnostic('Test diagnostic message in suite');
42+
43+
test('child test', () => {
44+
// Verify properties are accessible in nested test
45+
assert.strictEqual(typeof ctx.passed, 'boolean');
46+
assert.strictEqual(ctx.attempt, 0);
47+
});
48+
}));

0 commit comments

Comments
 (0)