Flaky Test
In one line
A flaky test is unreliable — it passes and fails randomly without code changes, eroding trust in the test suite.
In simple words
A flaky test is the enemy of a healthy test suite. It's a test that doesn't give consistent results — it might pass on your machine but fail in CI, or pass today and fail tomorrow, with no code changes.
Common causes of flakiness:
- Timing issues: relying on setTimeout or real time
- Shared state: tests that depend on execution order
- External dependencies: network calls, real database, file system
- Race conditions: async operations completing in different orders
- Random data: using Math.random() without seeding
Flaky tests are dangerous because they erode trust. If developers see a failed test and think 'oh, that one is just flaky,' they start ignoring failures — and real bugs slip through. Fix flaky tests immediately or quarantine them.
Example
// FLAKY: depends on timing
test('data loads within 100ms', () => {
const start = Date.now();
fetchData(); // might take 50ms or 150ms
expect(Date.now() - start).toBeLessThan(100);
});
// FIX: use fake timers or test behavior, not timingThis test is flaky because real timing varies. Use fake timers or test the result, not the duration.
Related terms
Related Resources
Related Lessons
- Test Best Practices
Writing maintainable tests.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.