Skip to content

Commit cdc5774

Browse files
MoLownodejs-github-bot
authored andcommitted
test_runner: add passed, attempt, and diagnostic to SuiteContext
PR-URL: #62504 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
1 parent c5a83ab commit cdc5774

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
@@ -4268,6 +4268,45 @@ added:
42684268

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

4271+
### `context.passed`
4272+
4273+
<!-- YAML
4274+
added: REPLACEME
4275+
-->
4276+
4277+
* Type: {boolean}
4278+
4279+
Indicates whether the suite and all of its subtests have passed.
4280+
4281+
### `context.attempt`
4282+
4283+
<!-- YAML
4284+
added: REPLACEME
4285+
-->
4286+
4287+
* Type: {number}
4288+
4289+
The current attempt number of the suite. Used in conjunction with the
4290+
`--test-rerun-failures` option to determine the attempt number of the current
4291+
run.
4292+
4293+
### `context.diagnostic(message)`
4294+
4295+
<!-- YAML
4296+
added: REPLACEME
4297+
-->
4298+
4299+
* `message` {string} A diagnostic message to output.
4300+
4301+
Output a diagnostic message. This is typically used for logging information
4302+
about the current suite or its tests.
4303+
4304+
```js
4305+
test.describe('my suite', (suite) => {
4306+
suite.diagnostic('Suite diagnostic message');
4307+
});
4308+
```
4309+
42714310
[TAP]: https://testanything.org/
42724311
[`--experimental-test-coverage`]: cli.md#--experimental-test-coverage
42734312
[`--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)