vitest-dev/vitest · error · TypeError
vi.unmock() expects a string path, but received a
Error message
vi.unmock() expects a string path, but received a ${typeof path} What it means
Thrown by `vi.unmock(path)` when `path` is not a string. Like `vi.mock`, `vi.unmock` is hoisted and requires a string module specifier so the transformer can register the unmock before imports execute. A non-string path is rejected with a TypeError.
Solutions
- Pass a string literal specifier: `vi.unmock('./myModule')`.
- If you need runtime control, use `vi.doUnmock` inside the test (still requires a string).
- Verify the argument is the specifier, not the imported module object.
Example fix
// before
import * as M from './myModule'
vi.unmock(M)
// after
vi.unmock('./myModule') Defensive patterns
Strategy: type-guard
Validate before calling
function unmockSafe(path) {
if (typeof path !== 'string') throw new TypeError('vi.unmock requires a string path')
return vi.unmock(path)
} Type guard
function isModulePath(v): v is string {
return typeof v === 'string' && v.length > 0
} Prevention
- Pass a string literal specifier to vi.unmock.
- Avoid passing imported bindings as the path.
- Use vi.doUnmock for non-hoisted, runtime-controlled unmocking.
When it happens
Trigger: Calling `vi.unmock(nonString)`; passing a module namespace object or Promise instead of a path string; using a dynamically computed path that isn't a string.
Common situations: Refactoring that replaces a literal path with a variable; passing the imported binding; misunderstanding the hoisting requirement; copy-paste from `vi.mock` with the wrong argument.
Related errors
- vi.mock() expects a string path, but received a
- vi.doMock() expects a string path, but received a
- vi.doUnmock() expects a string path, but received a
- vi.unmock() expects a string path, but received a
- any() expects to be passed a constructor function. Please…
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/d089461bdc8a2ed6.
Report an issue: GitHub.
Appendix: source
Thrown at packages/vitest/src/integrations/vi.ts:671
_mocker().queueMock(
path,
importer,
typeof factory === 'function'
? () =>
factory(() =>
_mocker().importActual(
path,
importer,
_mocker().getMockContext().callstack,
),
)
: factory,
)
},
unmock(path: string | Promise<unknown>) {
if (typeof path !== 'string') {
throw new TypeError(
`vi.unmock() expects a string path, but received a ${typeof path}`,
)
}
_mocker().queueUnmock(path, getImporter('unmock'))
},
doMock(path: string | Promise<unknown>, factory?: MockOptions | MockFactoryWithHelper) {
if (typeof path !== 'string') {
throw new TypeError(
`vi.doMock() expects a string path, but received a ${typeof path}`,
)
}
const importer = getImporter('doMock')
_mocker().queueMock(
path,
importer,
typeof factory === 'function'
? () =>View on GitHub (pinned to 1fa9837ec2)