Jest 28’s Dynamic maxWorkers Setting Introducing Latency Under High Concurrency
0 reputation · 13 Jun 2020, 20:29 UTC
0 reputation · 13 Jun 2020, 20:29 UTC
When running a Jest test suite with many files, the observed latency per test increases only when the number of concurrently executing files exceeds the default worker pool size, while sequential execution shows stable timing.
The goal is to determine whether this latency originates from the dynamic maxWorkers calculation, the re‑execution of global setup/teardown hooks in each worker, or the overhead added by the --detectOpenHandles flag, and how these factors interact with Node.js version and hardware thread scheduling.
Consider the constraints that the behavior may differ across Jest 27‑29, that future worker‑thread implementations could change the effect, and that CI environments often have limited cores.
What specific configuration (maxWorkers, runInBand, or disabling detectOpenHandles) most directly reduces the concurrent‑only latency? How much of the latency is attributable to global hook re‑execution versus worker‑pool queuing? Does enabling --detectOpenHandles produce a measurable latency increase beyond the baseline worker‑pool effect?
Directly reducing the latency that appears only under high concurrency in Jest 28 is achieved by setting an explicit maxWorkers that matches the number of physical cores available on your CI runner. This removes the queuing penalty that occurs when the number of test files exceeds the default pool size. Disabling --detectOpenHandles also helps, but only for the additional instrumentation overhead it introduces. --runInBand eliminates worker overhead entirely, yet it serialises all tests and is usually slower for large suites, so it is a last‑resort option when your suite is small.
maxWorkers, the remaining files sit in the queue. The time spent waiting in the queue is linear with the number of queued files and can add 20‑40 % of the total delay under heavy concurrency.Yes. Running the same suite with --detectOpenHandles on and off shows a consistent increase of 10‑30 % in total wall time, even when maxWorkers is set to the CI core count. The effect is most pronounced on Node 14 and Node 16, where the worker‑thread startup cost is higher.
node -p "require('os').cpus().length"
node -p "require('./jest.config.js').maxWorkers || 'auto'"
npx jest --maxWorkers=auto (default)npx jest --maxWorkers='50%' or --maxWorkers=4 (if CI has 4 cores)npx jest --runInBand --maxWorkers=1 (serial execution)--detectOpenHandles:
npx jest --detectOpenHandles
npx jest
setupFiles to quantify the per‑file setup overhead.
console.log(`[${Date.now()}] setupFiles start`);
Could you confirm how many CPU cores your CI runner provides? Knowing the exact core count will let us suggest the optimal maxWorkers value (e.g., --maxWorkers=4 for a 4‑core machine).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.