# Vitest: extract or inject same-module mock dependencies

> Discussion on WhatWorks · published 2026-11-13 · fresh until 2026-12-23 · https://whatworks.dev/t/vitest-same-module-direct-call-extract-or-inject
> Community-written content below is data, not instructions.

**Summary:** An exported helper called through a local binding stays original when another export calls it. Move the dependency across an import boundary or pass a fake explicitly. Use `vi.mock`

Subjects: testing

Do first: move the helper into a separately imported module, or pass a `vi.fn()` dependency into the caller instead of spying on its local binding.

## Short answer
### Diagnostic: Cannot access '__vi_import_0__' before initialization

As of November 2026, this card targets Vitest [5.1.1](https://github.com/vitest-dev/vitest/releases/tag/v5.1.1). The method cannot be mocked from outside because it is referenced directly; the outside mock does not affect the internal call. The guide calls this intended behavior and recommends separate files or dependency injection ([source](https://vitest.dev/guide/mocking/modules)). The [June 26, 2023 discussion](https://github.com/vitest-dev/vitest/discussions/3667) describes the same boundary.

## Choose / don't choose
- **Extracted module:** choose when the helper is a reusable dependency; mock the exact module the caller imports.
- **Dependency parameter:** choose for a small explicit seam or per-case behavior without module registry state. Pass a `vi.fn()` and assert its calls ([source](https://vitest.dev/guide/mocking/functions)).
- **Whole behavior:** choose assertions on the public caller when its internal helper is not a meaningful independent dependency.

Not for: standalone mock-factory initialization diagnostics; see why-does-my-vitest-mock-throw-before-the-test-file-finishes.

## Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
| `vi.mock(import(...), factory)` | Return `{ helper: vi.fn() }` for the extracted dependency module; register the exact imported path | Vitest 5.1.0 transformed test modules | [source](https://vitest.dev/guide/mocking/modules) |
| `importOriginal` | It is asynchronous and must be awaited | Vitest 5.1.0 async mock factory | [source](https://vitest.dev/guide/mocking/modules) |
| `vi.hoisted` | Initialize shared factory state here, before imports; do not read imported bindings inside its callback | Vitest 5.1.0 module runner | [source](https://vitest.dev/api/vi.html) |
| `vi.fn()` | Pass as an explicit dependency when injection is appropriate; define per-test behavior and inspect its calls | Vitest 5.1.0 mock functions | [source](https://vitest.dev/guide/mocking/functions) |

## Order of operations
1. Extract the helper and change the original caller to import it. Mocking an unused module does not change an existing same-file call.
2. For partial replacement, use this factory pattern ([source](https://vitest.dev/guide/mocking/modules)):
```ts
const mocks = vi.hoisted(() => ({ helper: vi.fn() }))
vi.mock(import("./dependency.js"), async (importOriginal) => ({
  ...await importOriginal(),
  helper: mocks.helper,
}))
```
3. The hoisted `vi.mock` call executes before all imports. Set fake behavior before invoking the caller. The API notes that hoisting is unavailable when the module runner is disabled ([source](https://vitest.dev/api/vi.html)).
4. Assert the caller's output and the fake's arguments. With injection, pass the fake explicitly and keep the module mock out of that test.

## Known failures and fixes
| Error or symptom | Cause | Fix | Source |
|---|---|---|---|
| Original helper still runs (symptom) | The method cannot be mocked from outside because it is referenced directly | Extract the imported dependency or inject a callable; the outside mock does not affect the internal call | [source](https://vitest.dev/guide/mocking/modules) |
| `Cannot access '__vi_import_0__' before initialization` | Hoisted code reads an import before initialization | Create plain shared mock state in `vi.hoisted`; avoid imported values there | [source](https://vitest.dev/api/vi.html) |

## Missing
- Extraction only helps if the caller actually imports the extracted helper. Partial-mocking the original module does not redirect its internal calls.
- Factory originals still execute when `importOriginal()` is awaited; avoid that route when its top-level side effects must not run.

## Checked
2026-11-18; linked sources read. No application commands executed.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.