mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
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: https://github.com/nodejs/node/pull/67312 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
This commit is contained in:
1 parent
4df7015560
commit
7be00a936d
4 files changed
+59
-1
No files matched your search
@@ -5139,6 +5139,32 @@ The attempt number of the suite. This value is zero-based, so the first attempt
|
||||
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.tags`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
> Stability: 1.0 - Early development
|
||||
|
||||
* Type: {string\[]}
|
||||
|
||||
A frozen array of the suite's flattened lowercased tags, in declaration
|
||||
order, including any tags inherited from ancestor suites. Empty when the
|
||||
suite has no tags. See [Test tags][].
|
||||
|
||||
### `context.workerId`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {number|undefined}
|
||||
|
||||
The unique identifier of the worker running the current test file. This value
|
||||
is derived from the `NODE_TEST_WORKER_ID` environment variable, and is
|
||||
`undefined` when not running in a test context.
|
||||
|
||||
### `context.diagnostic(message)`
|
||||
|
||||
<!-- YAML
|
||||
|
||||
@@ -536,6 +536,15 @@ class SuiteContext {
|
||||
return this.#suite.attempt ?? 0;
|
||||
}
|
||||
|
||||
get tags() {
|
||||
return this.#suite.tags;
|
||||
}
|
||||
|
||||
get workerId() {
|
||||
const envWorkerId = process.env.NODE_TEST_WORKER_ID;
|
||||
return Number(envWorkerId) || undefined;
|
||||
}
|
||||
|
||||
diagnostic(message) {
|
||||
this.#suite.diagnostic(message);
|
||||
}
|
||||
|
||||
+7
-1
@@ -1,4 +1,4 @@
|
||||
import { test } from 'node:test';
|
||||
import { describe, test } from 'node:test';
|
||||
|
||||
test('worker ID is available as environment variable', (t) => {
|
||||
const workerId = process.env.NODE_TEST_WORKER_ID;
|
||||
@@ -24,3 +24,9 @@ test('worker ID is available via context', (t) => {
|
||||
throw new Error(`context.workerId (${workerId}) should match NODE_TEST_WORKER_ID (${envWorkerId})`);
|
||||
}
|
||||
});
|
||||
|
||||
describe('worker ID is available via suite context', (s) => {
|
||||
if (s.workerId !== Number(process.env.NODE_TEST_WORKER_ID)) {
|
||||
throw new Error(`suite context.workerId (${s.workerId}) should match NODE_TEST_WORKER_ID (${process.env.NODE_TEST_WORKER_ID})`);
|
||||
}
|
||||
});
|
||||
@@ -46,6 +46,23 @@ test('multi-level nesting flattens by union', () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('suite context exposes inherited tags', () => {
|
||||
describe('outer', { tags: ['a'] }, () => {
|
||||
describe('mid', { tags: ['b'] }, (s) => {
|
||||
assert.deepStrictEqual(s.tags, ['a', 'b']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('child tags do not leak into the suite context', () => {
|
||||
describe('suite', { tags: ['a'] }, (s) => {
|
||||
it('child', { tags: ['b'] }, (t) => {
|
||||
assert.deepStrictEqual(t.tags, ['a', 'b']);
|
||||
assert.deepStrictEqual(s.tags, ['a']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('child without own tags inherits parent set unchanged', () => {
|
||||
describe('outer', { tags: ['db', 'fast'] }, () => {
|
||||
it('child', (t) => {
|
||||
|
||||
Reference in new issue
Block a user