diff --git a/doc/api/test.md b/doc/api/test.md index 257b17ae845f9c..e3a63eb354426b 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -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` @@ -4230,6 +4232,45 @@ added: Can be used to abort test subtasks when the test has been aborted. +### `context.passed` + + + +* Type: {boolean} + +Indicates whether the suite and all of its subtests have passed. + +### `context.attempt` + + + +* 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)` + + + +* `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 diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 97d53c097261d7..3c2b6ab3e92917 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -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) { diff --git a/test/parallel/test-runner-test-fullname.js b/test/parallel/test-runner-test-fullname.js index 5e8ad47bddbd87..cab35a7a9355aa 100644 --- a/test/parallel/test-runner-test-fullname.js +++ b/test/parallel/test-runner-test-fullname.js @@ -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'); @@ -26,3 +31,18 @@ suite('suite', common.mustCall((t) => { test((t) => { assert.strictEqual(t.fullName, ''); }); + +// 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); + }); +}));