-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
test_runner: expose worker ID for concurrent test execution #61394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Review requested:
|
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: nodejs#55842
863de78 to
606c1c7
Compare
JakobJingleheimer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still not sure this feature is necessary since it's easily facilitated in userland. But if it helps ease adoption from Jest and Mocha 🤔
|
|
||
| get workerId() { | ||
| const envWorkerId = process.env.NODE_TEST_WORKER_ID; | ||
| return envWorkerId !== undefined ? Number(envWorkerId) : undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can simplify this:
| return envWorkerId !== undefined ? Number(envWorkerId) : undefined; | |
| return Number(envWorkerId) || undefined; |
Number(undefined) → NaN (falsy)
envWorkerId should never be zero, so I that should be safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also that suggestion correctly handles empty string same as not defined, which is the convention for env variables
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing this out @JakobJingleheimer @aduh95 really appreciate the feedback
@JakobJingleheimer Thanks for taking a look! I totally get the hesitation about adding to core. The main issue is that while this is possible in userland, the workarounds are usually pretty messy or come with big trade-offs. Most people trying to migrate from Jest hit one of two walls: Unstable IDs: They try to use threadId/PID but that breaks because the IDs keep climbing (1, 2, 3 -> 99) as workers recycle, so you can't map them to a fixed set of databases Manual Sharding: They run multiple |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #61394 +/- ##
==========================================
- Coverage 88.51% 88.51% -0.01%
==========================================
Files 704 704
Lines 208797 208884 +87
Branches 40310 40329 +19
==========================================
+ Hits 184820 184896 +76
- Misses 15961 15974 +13
+ Partials 8016 8014 -2
🚀 New features to boost your workflow:
|
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:
Fixes: #55842