Jest · Unit tests that survive refactors

How does Jest run tests concurrently?

HardconcurrencyworkersmaxWorkersUpdated Oct 2026
Quiz this track

Short answer

Jest runs test files in parallel worker processes but runs tests inside a file sequentially. Control load with --maxWorkers, isolate shared resources, and never depend on cross-file test order.

Parallelism happens at the file level. Each worker gets its own environment and module registry, which is why module state does not leak between files. Inside a file, tests execute in order, so shared variables are still a hazard there. test.concurrent is an opt-in exception and changes that guarantee per block.

bash
npx jest --maxWorkers=50%

npx jest --runInBand
runInBand runs everything in one process, which is useful for debugging and for tests that cannot share a database.

What parallel files change

  • A fixed test database name collides across workers
  • A fixed port cannot be bound by two workers
  • A shared temp directory can be written by both
  • Ordering assumptions between files are invalid
  • A memory-heavy suite can exhaust CI resources at high worker counts

test.concurrent allows tests in a block to run at the same time. It is only safe when each test owns its state and no shared mutable mocks are involved. Most suites do not need it; file-level parallelism already uses the available cores. Use it when a block is dominated by independent async waits.

Flaky failures under parallelism usually indicate a shared resource, not a Jest bug. Run with --runInBand to confirm. If the suite passes serially, fix the isolation rather than lowering the worker count permanently.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Does Jest run tests within a file in parallel?
  2. 2When is test.concurrent safe?
  3. 3How do you diagnose a failure that only appears with multiple workers?
  4. 4Why does runInBand help when debugging?

Say it in the interview

Jest parallelises test files across worker processes, but tests inside a file run sequentially unless I opt into test.concurrent. That means shared state inside a file is still dangerous, and cross-file ordering is never safe. I control load with maxWorkers and use runInBand to debug. If a suite only passes serially, I treat it as a shared-resource bug and fix the isolation.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Jest mock interview and get scored feedback on depth, clarity and confidence.