Case file 03Testing8 min read

From one test file to 1,166, and the test runner that ate our laptops

Building a testing habit on a product with one test file, the setup that made tests cheap, and twelve Vitest processes filling a 24 GB machine.

Read by an AI voice of Sajib. Words light up as he reads.

In August, developers on the team started reporting the same problem. When they ran the test suite, their laptops froze. On my own Mac, twelve Vitest processes were running, each one using between 2.4 and 2.9 GB of memory. My 24 GB machine was almost full.

Five months earlier, that same product had one test file. Now it had so many tests that the test runner itself was the problem.

Here is how a testing habit grew on one product, from one file in March to 1,166 in October, and what made it stick. It covers the setup that made tests cheap to write, the runner that filled our laptops, a bug that only existed outside UTC, and how we later measured “how much is tested?” on a second product.

Starting with one test file

The product was about ten months old. It used Next.js and React on the frontend, with a lot of state around scheduling and publishing posts to social networks. Vitest, a fast test runner for JavaScript projects, was already installed, but there was only one test file. Everything else was checked by clicking around.

Asking people to write tests doesn’t work while every test is expensive to write. So I started with the setup, not the tests. On one day in March I added:

  • Mock Service Worker (MSW), a tool that catches network requests in tests and returns fake responses.
  • Data factories, small functions that create realistic test objects, like a fake user or a fake post.
  • A render helper that wraps a component in the same providers the real app uses, such as a fake login.
  • A global setup that replaces Next.js navigation with a fake version.

The same day, I wrote the first real tests. The suite went from 1 file to 29.

Some early tests recorded bugs instead of fixing them. One test name said, in effect, “this crash is known”. A test like that keeps a known bug in the suite, where nobody can lose track of it.

The real growth started in June. “Every fix ships with a test” became the rule I held my own pull requests to, and the team picked it up. Over the summer, more of the work ran through Claude Code, an AI coding agent. Because the setup made tests cheap, the agent wrote them too, and the suite grew with the code. By 20 October the product had 1,166 test files.

Fig. 3.1 · From one test file to 1,166 in about seven months.

The test runner that filled our laptops

My first idea for the frozen laptops was to limit how many workers Vitest could start. A worker is a separate process that runs some of the test files in parallel.

Before doing that, I measured, and that saved me from fixing the wrong thing. One full run, using every worker, took about 5 GB of memory and 65 seconds, and then exited cleanly. Parallel work was fine; the problem was processes that never ended, and three causes stacked up.

First, our test script started Vitest in watch mode. Watch mode keeps running and re-runs tests when files change, and it never exits on its own. A human presses Ctrl-C to stop it, but an AI agent, a timed-out tool call or a CI shell can’t. So every test run started by an agent left a full set of workers behind.

Second, Vitest sometimes left workers behind even in normal run mode. When the main process died, its workers kept running, with gigabytes of memory each. A process whose parent has died is called an orphan.

Third, I had several copies of the repository checked out, with several sessions testing at the same time. Orphans from all of them piled up until the machine ran out of memory.

Fig. 3.2 · Workers that outlive their main process keep their memory. Across several copies, they fill the machine.

The fix had three parts. A plain vitest command now runs once and exits. People who want watch mode use a separate test:watch script. And a small wrapper script cleans up orphaned workers before and after each run.

My first cleanup script was dangerous

My first cleanup script matched any Vitest process that belonged to the repository. It did not check whether that process still had a living parent.

That was a serious mistake. If two sessions were testing at once, the second session would kill the first session’s tests halfway through. The first session would then report a failure that had nothing to do with its code. I caught this the same day.

The second version looks at each group of workers, finds the top Vitest process in that group, and only cleans up the group if that top process has lost its parent. A healthy run always has a living parent, so the script never touches it.

// A group is orphaned when its top Vitest process has lost its parent.
function isOrphanedGroup(proc, processesByPid) {
  let root = proc;
  const visited = new Set();
  while (!visited.has(root.pid)) {
    visited.add(root.pid);
    const parent = processesByPid.get(root.ppid);
    if (!parent || !isTestWorker(parent)) break;
    root = parent;
  }
  return root.ppid === 1 || !processesByPid.has(root.ppid);
}

Running the script, and not only reading it, found one more bug. On macOS, the process name includes the full folder path. One checkout folder had “vitest” in its name, so the script also killed an unrelated build process that lived in that folder. I fixed the matching and added tests built from real process lines that I had seen.

To see whether the cleanup was still needed after watch mode was off, I turned it off for three full runs. Run mode alone still left orphans holding over 4 GB once, so the cleanup stayed.

A bug that only existed outside UTC

Later that month, a test file failed on my machine and passed in CI. The cause looked harmless: an afterAll block that tries to put the time zone back the way it was.

const originalTz = process.env.TZ; // usually undefined
afterAll(() => {
  process.env.TZ = originalTz; // stores the STRING "undefined"
});

// What it should be:
afterAll(() => {
  if (originalTz === undefined) delete process.env.TZ;
  else process.env.TZ = originalTz;
});

The TZ variable is usually not set. In Node.js, if you assign undefined to a process.env property, it stores the text “undefined”. Node can’t find a time zone with that name, so it uses UTC instead, for the rest of that worker process. Later tests in the same worker break.

My machine is in UTC+6, so the wrong time zone made a test fail. CI runs in UTC, where “fall back to UTC” looks exactly like the correct behaviour, so CI stayed green.

The same pattern was in two other files, so the fix went into all three.

Tests that can fail

A green test suite means something only if its tests can turn red. I kept finding tests that couldn’t.

One example was a function that limits video length. It had ten tests, and when I deleted the limit, all ten still passed, because every test started from the default value, where the limit does nothing. After I rewrote them, removing the limit made a test fail.

The check is simple: after a fix, undo the fix locally, run the test and watch it fail, then restore the fix. It takes about a minute, and it often catches tests that only look useful.

A cheap CI gate, and what it cost

The unit tests are the slowest job in CI. To save CI minutes, the product owner decided to run them only on release pull requests, not on feature pull requests. I documented what that would cost.

Feature pull requests could break tests without anyone noticing. In September I spent a whole pull request fixing 33 broken tests that came from six unrelated feature changes. Most releases still start with a cleanup like that.

The same habits on a second product

In October I brought these habits to a second product, an AI chat assistant. It had about 140 test files, but nobody could say how much of what users see was tested, and line coverage doesn’t answer that question.

So I built a map. With Claude Code agents doing much of the work, I listed every screen a guest, user or admin can reach, and every element and state on each screen. The map had 1,043 items. An item counted as tested only if a test checked the result, not just visited the page. Three AI graders checked each screen in different ways, and when they disagreed, the item was marked for a human to review.

At the start, only 7% of the items on clickable screens were fully tested.

Then agents wrote tests for those screens in parallel. Two days later, the number was 44%. The checks mattered more than the speed. A strict review pass found 38 weak tests, and a code review raised 49 findings, all worked through before merging.

Some new tests found real bugs in the app. I didn’t fix those quietly. Each one became an issue, with a test of the correct behaviour marked as an expected failure. Playwright’s test.fail passes while the bug exists. When someone fixes the bug, Playwright reports that the test “unexpectedly passed”, which forces the developer to remove the marker, and the test stays. Fifteen bugs were filed this way.

The day after this work, I also added lint, type checks and unit tests to every pull request on that product. Its CI had none of them before.

The chat assistant is only a third of the way to its goal of 90% of all items. The hardest parts, like the microphone, offline mode and admin tools, are planned but not done.

What I learned

A habit spreads when the right thing is also the cheap thing. The setup made tests cheap, and “every fix ships with a test” made them expected. The suite only took off once both were in place.

Build the test setup first. Factories, a render helper and default network mocks make the hundredth test as cheap as the first.

A test process that can’t exit on its own will one day be started by something that can’t stop it. Make “run once” the default, and make watch mode something you ask for.

Undo the fix and watch the test fail. If it doesn’t fail, the test isn’t checking the fix.

If I started again, I would run unit tests on every pull request from day one. I would also add one CI job that runs in a time zone other than UTC, which would have caught the “undefined” bug on the day it was written.

Product names and some details are changed or left out. The engineering is real.