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.
Contents
At Paymash, every code change has to pass 19,423 automated tests before it reaches a till. Since we switched from Jest to Vitest, this full quality check no longer takes 16:39 but 7:41 minutes – with the same tests against real databases. For our developers, that means feedback in minutes rather than after a coffee break. For our merchants: tested updates, faster.
Why speed means quality for us
A quality check is only useful if it actually runs. If it takes a quarter of an hour, you start it before heading home instead of after every change – and a bug only surfaces once nobody remembers where it came from. Under eight minutes, it runs before every commit.
What every change goes through at Paymash:
- All tests, not a selection. 19,423 tests, most of them sending real HTTP requests to a real PostgreSQL database instead of to mocks.
- An API contract that only grows. Installed POS apps often run unchanged for months. Every change to the interface is automatically checked against the published version; anything that would break existing apps stops the build.
- Security and traceability. An audit of our dependencies for known vulnerabilities, and every writing endpoint must show up in the audit log – otherwise the check fails.
So faster tests are not an end in themselves. They are the reason this discipline holds up day to day.
Starting point: tests against real databases
Paymash runs on a TypeScript backend that serves the POS, self-ordering, webshops and accounting. A bug in it can, at worst, cost a merchant an evening’s revenue. That is why every change has to pass 19,423 automated tests in 1,626 test files.
Most of these 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).
That gives us confidence, but it comes at a price: the full quality check – formatting, build, security audit, linting, API contract and all tests with coverage – took 999 seconds with Jest, a good 16 minutes. When you have to wait that long, you run the check less often.
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. The biggest risk in a switch like this is a test that stays green afterwards but no longer checks anything – for example because a mock no longer takes effect.
So we migrated in ten stages, each its own commit. After each stage, a comparison tool ran over both test runs:
- the same test names with the same result (passed, skipped, todo),
- the same statements, functions and branches executed in production code.
It paid off. One example: Jest and Vitest name stored snapshots differently. Without renaming them, Vitest would have written fresh snapshots on every run and compared them against nothing – green tests that check nothing. The comparison showed this immediately.
Finally, we compared the coverage of the entire test run line by line with the last Jest run: not a single line that was tested under Jest is untested under Vitest.
Loading modules once instead of 1,626 times
Jest gives every test file a fresh set of all modules. That is safe but expensive: 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.
The catch: if a test file replaces a module with a mock (vi.mock), that 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 detects isolated files on its own – by any call that replaces or resets modules. Nobody has to maintain a list, and a new test file with a mock lands in the right group by itself. Both groups run one after the other and use the same databases per process without ever 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 just 677.
5½ minutes idle after the last test
After the switch, something stood out: the last test had finished, the CPUs were idle – and the summary only appeared five and a half minutes later. We found the cause with DEBUG=vitest:coverage: “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. This is exactly where our gain from the previous section worked against us: a process that keeps modules across many files lists practically the whole application in every report, almost all of it with a count of zero.
The solution comes from a small observation: merging means adding up counters, and adding zero changes nothing. We wrap 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 – Vitest reports files that no test touches separately as untested anyway.
The result
The full quality check now runs in less than half the time – 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 that, the tests themselves take 382 seconds, and merging the coverage just 21. The rest is build, audit, linting and the API contract check.
What didn’t get better – and what we learned
The coverage figure dropped by just under one point, but no less is being tested. Both tools measure with Istanbul, but over different code: Jest transpiled our modules to CommonJS and added around 15% extra import and export statements in the process, almost all of which run on load. That raised the percentage. The line-by-line comparison shows that the same code is covered.
Retries stay switched on. With twelve parallel processes loading HTTP servers and databases at the same time, the odd request hangs in every run. It was just the same under Jest. So a test may be retried up to twice.
What we take away:
- Measure before you optimise. The switch didn’t only bring speed in itself, it also exposed a bottleneck nobody had suspected: the step after the last test.
- A migration needs proof, not a green tick. Only the test-by-test comparison found tests that were green and no longer checked anything.
- Rules belong in the configuration, not in a list. Because test files with mocks run in isolation automatically, a whole class of hard-to-find bugs can’t happen.
In the end, only one thing matters to our merchants: that the till works when the shop is full. A fast, complete quality check is our tool for that – and with the switch to Vitest, it has become twice as fast.
Start selling with Paymash today
Set up in minutes. First sale the same day. No credit card required.
Start Free Trial