From Jest to Vitest: 19,423 tests in less than half the time

How Paymash runs 19,423 tests against real databases in less than half the time: the switch from Jest to Vitest and a 30-line fix for coverage.

José Manuel Rodriguez 6 min read
Full quality check: Jest 16:39 min, Vitest 7:41 min (−54%)
Contents
  1. What every change goes through
  2. Switching without losing a single test
  3. Loading modules once instead of 1,626 times
  4. 5½ minutes idle after the last test
  5. The result
  6. What didn’t get better

At Paymash, every code change runs through 19,423 automated tests before it reaches a till. With Jest, the full check took 16:39 minutes. Since we switched to Vitest it takes 7:41, with the same tests against real databases. Five of those minutes came, in the end, from a 30-line fix for coverage.

What every change goes through

Paymash runs on a TypeScript backend that serves the POS, self-ordering, webshops and accounting. Before a change to it ships, we run formatting, the build, linting, an audit of our dependencies for known vulnerabilities, and all 19,423 tests in 1,626 test files. Every change to the interface is also checked against the published version, because installed POS apps often run unchanged for months, and anything that would break them stops the build. The check also fails if a writing endpoint doesn’t show up in the audit log.

Most of the tests are not isolated unit tests with mocks. They send real HTTP requests to the API and check the result in a real PostgreSQL database. To do this, each of the twelve parallel test processes starts its own database container (Testcontainers).

With Jest, all of that took 999 seconds. When a check takes a quarter of an hour, you start it before heading home rather than after every change, and a bug only surfaces once nobody remembers where it came from. At under eight minutes, we run it before every commit.

Switching without losing a single test

The move to Vitest was part of a larger overhaul: our code now runs as a native ES module and is built with TypeScript 7. What worried us most was a test that stays green after the switch but no longer checks anything, for example because a mock no longer takes effect.

So we migrated in ten stages, each in its own commit. After each stage, a comparison tool put the two test runs side by side. They had to produce the same test names with the same result (passed, skipped, todo), and the same statements, functions and branches had to run in production code.

The comparison caught one case right away. Jest and Vitest name stored snapshots differently, and without renaming them Vitest would have written fresh snapshots on every run and never compared them with the old ones. Those tests would have stayed green while checking nothing.

Finally, we compared the coverage of the entire test run line by line with the last Jest run. Every line that was tested under Jest is also tested under Vitest.

Loading modules once instead of 1,626 times

Jest gives every test file a fresh set of all modules, so every file loads our application from scratch. Vitest can keep modules in one process across several test files (isolate: false). In our measurements, this saved around 35% compared with isolating each file.

That only works as long as no test file replaces a module with a mock (vi.mock), because the mock would leak into every following file in the same process. So we split the tests automatically into two groups:

Group Files Tests Modules
shared 1,565 18,627 loaded once per process
isolated 61 796 fresh per test file

The configuration searches every test file for calls that replace or reset modules and isolates those files. Nobody has to maintain a list, and a new test file with a mock lands in the right group by itself. The two groups run one after the other and share the databases of each process without getting in each other’s way.

Together with Vitest’s faster loading and transpiling, the summed run time per test file fell to about a third. The invoice tests alone went from 2,020 seconds to 677.

5½ minutes idle after the last test

After the switch we noticed something odd. The last test had finished and the CPUs were idle, yet the summary only appeared five and a half minutes later. DEBUG=vitest:coverage showed why: “Generate coverage total time 321729 ms”.

After each test file, Vitest records which lines of code it executed, and at the end it merges all 1,626 reports on a single thread. The trouble came from the optimisation in the previous section. A process that keeps modules across many files lists almost the whole application in every report, and nearly every counter in it is zero.

Merging adds counters up, and adding zero changes nothing. So we wrapped Vitest’s coverage provider in around 30 lines that drop every file without a single hit before each report:

takeCoverage(options) {
  const store = globals[STORE_KEY];
  const ran = {};
  for (const file in store) if (executed(store[file])) ran[file] = store[file];
  globals[STORE_KEY] = ran;            // Istanbul only reads what ran
  try { return istanbul.takeCoverage(options); }
  finally { globals[STORE_KEY] = store; } // restore the real counters
}

Merging now takes 21 seconds instead of 322. The coverage figures are identical down to the decimal place, since Vitest reports files that no test touches separately as untested anyway.

The result

We measured several times on the same machine (Apple M5 Pro, twelve parallel test processes):

Jest Vitest
Full quality check (13 stages) 999 s 461 s
Tests 19,422 19,423
Test files 1,624 1,626
Statement coverage 92.5% 91.6%

Of the 461 seconds, the tests themselves take 382 and merging the coverage takes 21. The rest is build, audit, linting and the API contract check.

What didn’t get better

The coverage figure dropped by just under one point, although we test no less than before. Both tools measure with Istanbul, but over different code. Jest transpiled our modules to CommonJS and in the process added around 15% extra import and export statements, almost all of which run on load. That raised the percentage. The line-by-line comparison shows that the same code is covered.

Retries are still switched on. With twelve parallel processes loading HTTP servers and databases at the same time, the odd request hangs in every run. It was the same under Jest, so a test may be retried up to twice.

If you use Vitest with isolate: false and coverage, run it once with DEBUG=vitest:coverage and look at how long the merge takes. For us it was over five minutes, almost as long as all the tests together, and nobody had suspected that step.

Start selling with Paymash today

Set up in minutes. First sale the same day. No credit card required.

Start Free Trial

Start selling with Paymash today

Start Free Trial

Start selling with Paymash today

Set up in minutes. First sale the same day. No credit card required.

No sign up, with sample data