Skip to content

Expose an id for concurrent test runners (like JEST_WORKER_ID) #55842

Description

@blimmer

What is the problem this feature will solve?

When running tests concurrently (the default setting in the Node test runner), it's common practice to split concurrent test runs across multiple local resources (like locally running servers, databases, etc).

Many other popular test runners offer this feature via an environment variable:

This makes it very easy to set up the proper database connection. For example, here's a snippet from my codebase:

if (process.env.STAGE === "test" && process.env.JEST_WORKER_ID) {
  process.env.DB_DATABASE = `myapp_tests_${process.env.JEST_WORKER_ID}`;
}

Then, in my database docker container, I create n databases, one for each worker. For example, if I run jest with --maxWorkers 8, I create myapp_tests_1 -> myapp_tests_8. During tests, the parallel test suites talk to a different database to avoid deadlocks.

What is the feature you are proposing to solve the problem?

The simplest solution for users coming from other frameworks would be to provide an environment variable just like the other test runners (e.g., NODE_TEST_WORKER_ID).

However, if that's not feasible, adding a workerId to the TestContext could also be a good solution. This solution is not as ideal as the environment variable because people would need to pass the test context to whatever establishes their db/server/etc connection. In my case, at least, this would require refactoring of code. Right now, I rely on that variable being set at file import time.

What alternatives have you considered?

I considered trying to find a solution based on pid, but it's not reliable enough. @redyetidev mentioned in Slack that there might be a solution using hooks. I have not dug into this yet.

Activity

  1. added
    test_runnerIssues and PRs related to the test runner subsystem.
    on Nov 13, 2024
  2. moved this from Awaiting Triage to Triaged in Node.js feature requestson Nov 13, 2024
  3. cjihrig commented on Nov 16, 2024

    @cjihrig
    Contributor

    We could add an environment variable fairly easily, but I have a question:

    if (process.env.STAGE === "test" && process.env.JEST_WORKER_ID) {
      process.env.DB_DATABASE = `myapp_tests_${process.env.JEST_WORKER_ID}`;
    }

    There is already a NODE_TEST_CONTEXT environment variable that can be used to determine that the test runner is being used (if you actually need something like that). Why not use something like crypto.randomUUID() to create a unique ID for each file?

  4. blimmer commented on Nov 16, 2024

    @blimmer
    Author

    The problem is that, as an external step before the test runner is launched, I:

    • Start up the postgres instance via docker compose
    • Run an init script to create n databases (based on the parallelism I run the tests with)
    • Run my migration logic (node-pg-migrate) against all the test databases
    • Create a flush_database() function in each test database to run beforeEach test

    I don't want to do this for each test file because the migration logic is quite slow. It's much faster to migrate once and have a fast flush_database function to reset database state between tests.

    So a UUID, for my use case, would not be appropriate. Having a sequential integer between 1 and n (where n is the max concurrency) is required for the database setup logic that occurs outside the test runner context.

  5. cjihrig commented on Nov 16, 2024

    @cjihrig
    Contributor

    Got it. Thanks for the explanation. I think we should add support for this.

  6. added
    good first issueIssues that are suitable for first-time contributors.
    on Nov 17, 2024
  7. 0hmX commented on Nov 18, 2024

    @0hmX
    Contributor

    I can work on this! will pick this up!

  8. cjihrig commented on Nov 18, 2024

    @cjihrig
    Contributor

    Thanks @cu8code. Please be sure to handle both cases of test isolation. When --experimental-test-isolation=process, each child process should get an environment variable from 1 to N. When --experimental-test-isolation=none, only the value of 1 should be used.

  9. hpatel292-seneca commented on Nov 22, 2024

    @hpatel292-seneca

    Hi @cu8code, are you working on this? If not I can give it a try.

  10. 0hmX commented on Nov 22, 2024

    @0hmX
    Contributor

    I am working on this 👍

  11. JakobJingleheimer commented on Dec 1, 2024

    @JakobJingleheimer
    Member

    Is worker.threadId possibly what you're looking for?

  12. blimmer commented on Dec 1, 2024

    @blimmer
    Author

    We discussed threadId in Slack. It's not quite equivalent to what's available in other test runners:

    Screenshot 2024-12-01 at 09 36 27

  13. 18 remaining items

  14. thisalihassan commented on Jan 15, 2026

    @thisalihassan
    Contributor

    I've opened PR #61394 to address this issue.

    The PR adds NODE_TEST_WORKER_ID environment variable and context.workerId property, which are set based on the test isolation mode:

    • --test-isolation=process (default): Each test file gets a unique worker ID from 1 to N, where N is the concurrency level
    • --test-isolation=none: All tests get worker ID 1 (since they run in the same process)

    Worker IDs are managed through a pool that allocates and reuses IDs as test files complete, so with 16 test files and concurrency of 8, IDs 1-8 get reused twice.

    Why Sequential IDs (1, 2, 3...) vs UUIDs:

    I considered using crypto.randomUUID() as suggested in the comments, but went with sequential IDs for consistency with other test frameworks:

    • Jest uses JEST_WORKER_ID (sequential)
    • Vitest uses VITEST_POOL_ID (sequential)
    • Mocha uses MOCHA_WORKER_ID (sequential)

    Sequential IDs also make it easier to pre-allocate resources.

    This is available at import time, so it works for module-level initialization without needing to pass context around.
    Looking forward to feedback on the implementation!

  15. zhanglinqian commented on Feb 12, 2026

    @zhanglinqian

    I'd like to work on this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.good first issueIssues that are suitable for first-time contributors.test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions