CodePark/ questions / Isolation, parallelism & flaky tests
START PRACTICINGPRACTICE
Backend question · Backend testing › Isolation, parallelism & flaky tests

What are the common causes of flaky tests in a backend codebase, and how do you fix them?

A flaky test is a test that passes and fails on the same code. Common causes:

  • Shared state left by other tests: rows, cache keys, singletons, or mocks that were not reset.
  • Order dependence: a test only passes if other tests run before it.
  • Time: Date.now(), midnight, time zones, or real sleeps.
  • Randomness without a seed.
  • Network calls to real services.
  • Race conditions: for example, the test checks the result before a job or event has finished.
  • Resource clashes: fixed ports, or one database shared by parallel workers.

Fixes:

  • Give each test its own data, and reset state in hooks like beforeEach and afterEach.
  • Control time with fake timers or an injected clock.
  • Seed randomness.
  • Stub the network.
  • Wait for a condition with a timeout instead of sleeping for a fixed time.
  • Use random ports and a separate database per worker.

Treat flakiness as a bug. Quarantine the test, which means take it out of the main run, and give it an owner. Then fix the cause. Don't hide it behind endless retries.

  • Shared state and order dependence cause many flaky tests.
  • Time, unseeded randomness and real network calls cause flakiness.
  • For races, wait for a condition, not a fixed sleep.
  • Parallel workers can clash over ports and databases.
  • Quarantine and fix a flaky test rather than retrying it forever.