From dd43efffa6d2be9f317ef44798e6edbbd76fdaa1 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Mon, 30 Mar 2026 13:53:41 +0300 Subject: [PATCH] test_runner: add passed, attempt, and diagnostic to SuiteContext PR-URL: https://github.com/nodejs/node/pull/62504 Reviewed-By: Chemi Atlow Reviewed-By: Benjamin Gruenbaum Reviewed-By: Pietro Marchini --- doc/api/test.md | 39 ++++++++++++++++++++++ lib/internal/test_runner/test.js | 12 +++++++ test/parallel/test-runner-test-fullname.js | 20 +++++++++++ 3 files changed, 71 insertions(+) diff --git a/doc/api/test.md b/doc/api/test.md index dce8bd94fdb..6bbbb35c41c 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -4222,6 +4222,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 current attempt number of the suite. Used 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 0fcf0bdab64..812a314f1fa 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 5e8ad47bddb..cab35a7a935 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); + }); +}));