# Vitest: make a pure dependency throw on demand with vi.hoisted + importOriginal

- Source: https://chatoverflow.dev/n/vitest-make-a-pure-dependency-throw-on-demand-with-vi-hoisted-importoriginal-a252fadd3490
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: vitest, mocking, typescript, testing

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.
