How we went wrong
Every change to Otto waits on the test suite before it lands. At the start of this audit the suite took 8 minutes 43 seconds on an 8-core machine: 572 files, 5,766 tests. Typecheck, file-length checks and the build together took about 50 seconds, so the tests were roughly 90% of the wait. We set out to find why, and mostly found our own habits.
What was slow
Summed over the workers, the suite spent 1,728 seconds running tests. Three things explained most of it.
- A convention we copied everywhere. Early on, a test needed a fresh data directory, so it reset the module registry and re-imported the code under test before every test. That was reasonable once. It became the house style, and 171 files did it. Each test then paid for a cold transform of a large module graph. This was about half of all the avoidable time.
- Parity tests on a development build. Nine test files render thousands of seeded UI pairs to prove that moved components still produce identical markup. The slowest single test took 65 seconds. A CPU profile showed about 60% of it inside React's development-mode bookkeeping.
- Real waits. Some tests sleep for timers the code under test would run itself.
What we got wrong
We never measured. The default five-second timeout turned the cold-import cost into occasional flakes, and we treated them as flakes instead of asking why a test took five seconds. Nobody profiled the loops of thousands of renders. Four tests depended on the machine they ran on (file-creation mask, git identity), and a second test configuration for our mobile app was outside both the default run and CI, so 37 files were never run.
What fixed it
No product code changed, and the test count stayed identical at 5,766.
- We moved the parity loops to React's production build: the worst file went from 64 to 21 seconds, and the nine files together saved well over 100 seconds of worker time.
- We hoisted the per-test imports into one import per file where nothing leaked between tests, and kept the reset only where module state really carries over. Files typically halved; one went from about 31 seconds to 5.
- We used fake timers for waits that only let the client's own timers elapse, and left real waits where a real process is involved.
- We made tests set what they depend on, and added the mobile app's tests to the default run.
The result
The suite now takes 5 minutes 8 seconds instead of 8:43. Summed test time fell from 1,728 to 875 seconds, about half, on a shared machine that was no quieter than for the first measurement. Same files, same tests, same four environmental failures that the follow-ups later fixed.
What we keep
A convention written to solve one problem should say what it costs. Look at a test's duration when it times out. Profile anything that loops thousands of times. And a test should set the conditions it needs instead of trusting the machine.