From 897df8b406966c0d1eec940d790a114288d6e3eb Mon Sep 17 00:00:00 2001 From: Ali Hassan Date: Fri, 16 Jan 2026 03:10:42 +0500 Subject: [PATCH] test_runner: expose worker ID for concurrent test execution 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 --- doc/api/test.md | 33 ++++ lib/internal/test_runner/runner.js | 51 +++++++ lib/internal/test_runner/test.js | 5 + .../fixtures/test-runner/worker-id/test-1.mjs | 26 ++++ .../fixtures/test-runner/worker-id/test-2.mjs | 26 ++++ .../fixtures/test-runner/worker-id/test-3.mjs | 26 ++++ test/parallel/test-runner-worker-id.js | 141 ++++++++++++++++++ 7 files changed, 308 insertions(+) create mode 100644 test/fixtures/test-runner/worker-id/test-1.mjs create mode 100644 test/fixtures/test-runner/worker-id/test-2.mjs create mode 100644 test/fixtures/test-runner/worker-id/test-3.mjs create mode 100644 test/parallel/test-runner-worker-id.js diff --git a/doc/api/test.md b/doc/api/test.md index 29d071e668b8c6..be5c63c6e8e613 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -3720,6 +3720,39 @@ added: v25.0.0 Number of times the test has been attempted. +### `context.workerId` + + + +* 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. When running tests +with `--test-isolation=process` (the default), each test file runs in a separate +child process and is assigned a worker ID from 1 to N, where N is the number of +concurrent workers. When running with `--test-isolation=none`, all tests run in +the same process and the worker ID is always 1. This value is `undefined` when +not running in a test context. + +This property is useful for splitting resources (like database connections or +server ports) across concurrent test files: + +```mjs +import { test } from 'node:test'; +import { process } from 'node:process'; + +test('database operations', async (t) => { + // Worker ID is available via context + console.log(`Running in worker ${t.workerId}`); + + // Or via environment variable (available at import time) + const workerId = process.env.NODE_TEST_WORKER_ID; + // Use workerId to allocate separate resources per worker +}); +``` + ### `context.plan(count[,options])`