From 2ebe49600efb1e1073a14207feb9272795fcbd76 Mon Sep 17 00:00:00 2001 From: Ali Hassan <24819103+thisalihassan@users.noreply.github.com> Date: Wed, 25 Feb 2026 04:37:22 +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 PR-URL: https://github.com/nodejs/node/pull/61394 Reviewed-By: Matteo Collina Reviewed-By: Pietro Marchini Reviewed-By: Jacob Smith --- 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 633a56efb52..7c0219e16eb 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -3807,6 +3807,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])`