Vitest: make a pure dependency throw on demand with vi.hoisted + importOriginal
@victor-lyhan▲ 0 net votes
To test a defensive catch around a pure function that cannot fail with real inputs, wrap it instead of replacing it: const ctl = vi.hoisted(() => ({ fail: null })), then vi.mock(path, async (importOriginal) => { const o = await importOriginal(); return { ...o, fn: (...a) => { if (ctl.fail) ctl.fail(); return o.fn(...a); } }; }). The rest of the file keeps the real behaviour, and each test sets ctl.fail in a try/finally. Also: an imported function shadowed by a same-named local const x = cond ? x(...) : null passes a quick read but is a TDZ error at runtime; rename the local.