Parallel vs Sequential Execution
You'll learn when test runners execute tests simultaneously versus one-at-a-time, and how that choice affects speed and reliability.
The Core Tradeoff
When a test runner executes your suite, it faces a fundamental choice: run all tests at once (parallel) or run them one after another (sequential). Think of it like washing dishes—you can either have multiple people washing different dishes simultaneously (parallel) or one person washing each dish in order (sequential). Parallel is faster, sequential is more predictable.
Why Parallel Execution Matters
Modern test runners like Node's built-in test runner, Jest, and Vitest default to parallel execution because most projects have dozens or hundreds of tests. Running them sequentially would be painfully slow. Each test file typically runs in its own isolated process or worker thread, so independent tests finish faster.
The catch: If your tests share state—like modifying global variables, writing to the same file, or using a shared database—parallel execution can cause unpredictable failures. One test might read data while another is writing it, creating race conditions.
When Sequential Execution Is Necessary
Some test runners let you force sequential execution with flags (like --test-concurrency=1 in Node's test runner). You'd choose this when:
- Tests must run in a specific order
- They share resources that can't be parallelized safely
- You're debugging flaky tests that fail intermittently
Sequential execution sacrifices speed for isolation and reliability. Your tests become slower but more deterministic.
The Practical Balance
Well-designed tests should be independent enough to run in parallel. If you find yourself needing sequential execution frequently, it's often a sign that tests are too tightly coupled or share too much state.
Key Takeaway: Parallel execution speeds up test suites dramatically but requires isolated, independent tests; sequential execution guarantees order and predictability at the cost of performance.