A Mock That Imported the Real Thing

A flaky error-reporting test traced to a mock factory that quietly resolved the real Sentry package under full-suite load, and the audit for the same pattern elsewhere.

One test in the error-reporting suite started failing under full-suite runs, never in isolation: a hard timeout at exactly five seconds, twice in one session, always the same test. Isolated, it passed every time, three separate confirmations. That gap, reliable alone and flaky only under load, pointed straight at contention rather than the assertion itself.

The mock for the dynamically-imported Sentry SDK was the actual cause. Its factory called into Vitest’s real-module loader to spread the genuine package’s other exports before overriding the two functions the test cared about, which meant the real, fairly heavy package still had to be resolved and transformed the first time any test in the file ran. Alone, nothing else competed for that work and it finished well under the default timeout. Under the full suite, roughly two hundred other files transforming and importing at once occasionally pushed that one resolution past five seconds, and the test that happened to be waiting on it timed out.

The fix removed the dependency on real-module resolution speed entirely, hand-writing the small set of exports the test actually touches instead of spreading the real ones:

// before
const actual = await vi.importActual('@sentry/sveltekit');
return { ...actual, captureException, withScope };

// after
return { captureException, withScope };

An explicit ten-second timeout went on the two assertions that had been inheriting the default, a cheap second line of defense that does not depend on the mock ever staying this narrow. A pass over the rest of the suite for the same shape, a mock factory reaching into a real module it did not need, found nothing else doing it: this was the only test carrying an unnecessary real import into what should have been a pure fake.