-
-
Notifications
You must be signed in to change notification settings - Fork 35.3k
Expose an id for concurrent test runners (like JEST_WORKER_ID) #55842
Copy link
Copy link
Closed
Labels
feature requestIssues that request new features to be added to Node.js.Issues that request new features to be added to Node.js.good first issueIssues that are suitable for first-time contributors.Issues that are suitable for first-time contributors.test_runnerIssues and PRs related to the test runner subsystem.Issues and PRs related to the test runner subsystem.
Metadata
Metadata
Assignees
Labels
feature requestIssues that request new features to be added to Node.js.Issues that request new features to be added to Node.js.good first issueIssues that are suitable for first-time contributors.Issues that are suitable for first-time contributors.test_runnerIssues and PRs related to the test runner subsystem.Issues and PRs related to the test runner subsystem.
Type
Projects
Status
Triaged
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:
jest:JEST_WORKER_IDmocha:MOCHA_WORKER_IDvitest:VITEST_POOL_IDThis makes it very easy to set up the proper database connection. For example, here's a snippet from my codebase:
Then, in my database docker container, I create
ndatabases, one for each worker. For example, if I runjestwith--maxWorkers 8, I createmyapp_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
workerIdto theTestContextcould 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 usinghooks. I have not dug into this yet.