This adds support for identifying which worker is running a test file when tests execute concurrently, similar to JEST_WORKER_ID in Jest, VITEST_POOL_ID in Vitest, and MOCHA_WORKER_ID in Mocha. When running with --test-isolation=process (default), each test file runs in a separate child process and receives a unique worker ID from 1 to N. When running with --test-isolation=none, all tests run in the same process and the worker ID is always 1. This enables users to allocate separate resources (databases, ports, etc.) for each test worker to avoid conflicts during concurrent execution. Changes: - Add WorkerIdPool class to manage worker ID allocation and reuse - Set NODE_TEST_WORKER_ID environment variable for child processes - Add context.workerId getter to TestContext class - Add tests for worker ID functionality - Add documentation for context.workerId Fixes: https://github.com/nodejs/node/issues/55842 PR-URL: https://github.com/nodejs/node/pull/61394 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
26 lines
744 B
JavaScript
26 lines
744 B
JavaScript
import { test } from 'node:test';
|
|
|
|
test('worker ID is available as environment variable', (t) => {
|
|
const workerId = process.env.NODE_TEST_WORKER_ID;
|
|
if (workerId === undefined) {
|
|
throw new Error('NODE_TEST_WORKER_ID should be defined');
|
|
}
|
|
|
|
const id = Number(workerId);
|
|
if (isNaN(id) || id < 1) {
|
|
throw new Error(`Invalid worker ID: ${workerId}`);
|
|
}
|
|
});
|
|
|
|
test('worker ID is available via context', (t) => {
|
|
const workerId = t.workerId;
|
|
const envWorkerId = process.env.NODE_TEST_WORKER_ID;
|
|
|
|
if (workerId === undefined) {
|
|
throw new Error('context.workerId should be defined');
|
|
}
|
|
|
|
if (workerId !== Number(envWorkerId)) {
|
|
throw new Error(`context.workerId (${workerId}) should match NODE_TEST_WORKER_ID (${envWorkerId})`);
|
|
}
|
|
});
|