Skip to content

Commit 7be00a9

Browse files
sangwook-devaduh95
authored andcommitted
test_runner: add tags and workerId to SuiteContext
TestContext exposes tags and workerId, but SuiteContext does not, so a describe() callback cannot read the suite's tags, including the ones inherited from ancestor suites. Expose both on SuiteContext as well. Signed-off-by: sangwook <rewq5991@gmail.com> PR-URL: #67312 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 4df7015 commit 7be00a9

4 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎doc/api/test.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5139,6 +5139,32 @@ The attempt number of the suite. This value is zero-based, so the first attempt
51395139
the second attempt is `1`, and so on. This property is useful in conjunction with the
51405140
`--test-rerun-failures` option to determine the attempt number of the current run.
51415141

5142+
### `context.tags`
5143+
5144+
<!-- YAML
5145+
added: REPLACEME
5146+
-->
5147+
5148+
> Stability: 1.0 - Early development
5149+
5150+
* Type: {string\[]}
5151+
5152+
A frozen array of the suite's flattened lowercased tags, in declaration
5153+
order, including any tags inherited from ancestor suites. Empty when the
5154+
suite has no tags. See [Test tags][].
5155+
5156+
### `context.workerId`
5157+
5158+
<!-- YAML
5159+
added: REPLACEME
5160+
-->
5161+
5162+
* Type: {number|undefined}
5163+
5164+
The unique identifier of the worker running the current test file. This value
5165+
is derived from the `NODE_TEST_WORKER_ID` environment variable, and is
5166+
`undefined` when not running in a test context.
5167+
51425168
### `context.diagnostic(message)`
51435169

51445170
<!-- YAML

‎lib/internal/test_runner/test.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,15 @@ class SuiteContext {
536536
return this.#suite.attempt ?? 0;
537537
}
538538

539+
get tags() {
540+
return this.#suite.tags;
541+
}
542+
543+
get workerId() {
544+
const envWorkerId = process.env.NODE_TEST_WORKER_ID;
545+
return Number(envWorkerId) || undefined;
546+
}
547+
539548
diagnostic(message) {
540549
this.#suite.diagnostic(message);
541550
}

‎test/fixtures/test-runner/worker-id/test-1.mjs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test } from 'node:test';
1+
import { describe, test } from 'node:test';
22

33
test('worker ID is available as environment variable', (t) => {
44
const workerId = process.env.NODE_TEST_WORKER_ID;
@@ -24,3 +24,9 @@ test('worker ID is available via context', (t) => {
2424
throw new Error(`context.workerId (${workerId}) should match NODE_TEST_WORKER_ID (${envWorkerId})`);
2525
}
2626
});
27+
28+
describe('worker ID is available via suite context', (s) => {
29+
if (s.workerId !== Number(process.env.NODE_TEST_WORKER_ID)) {
30+
throw new Error(`suite context.workerId (${s.workerId}) should match NODE_TEST_WORKER_ID (${process.env.NODE_TEST_WORKER_ID})`);
31+
}
32+
});

‎test/parallel/test-runner-tags-inheritance.mjs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ test('multi-level nesting flattens by union', () => {
4646
});
4747
});
4848

49+
test('suite context exposes inherited tags', () => {
50+
describe('outer', { tags: ['a'] }, () => {
51+
describe('mid', { tags: ['b'] }, (s) => {
52+
assert.deepStrictEqual(s.tags, ['a', 'b']);
53+
});
54+
});
55+
});
56+
57+
test('child tags do not leak into the suite context', () => {
58+
describe('suite', { tags: ['a'] }, (s) => {
59+
it('child', { tags: ['b'] }, (t) => {
60+
assert.deepStrictEqual(t.tags, ['a', 'b']);
61+
assert.deepStrictEqual(s.tags, ['a']);
62+
});
63+
});
64+
});
65+
4966
test('child without own tags inherits parent set unchanged', () => {
5067
describe('outer', { tags: ['db', 'fast'] }, () => {
5168
it('child', (t) => {

0 commit comments

Comments
 (0)