vitest-dev/vitest · error · TypeError
vi.unmock() expects a string path, but received a ${typeof p
Error message
vi.unmock() expects a string path, but received a ${typeof path} What it means
Thrown at packages/mocker/src/browser/hints.ts:85-89 as a TypeError when vi.unmock() receives a non-string first argument. vi.unmock queues removal of a mock by module specifier, so the path must be a string to look up in the registry.
Source
Thrown at packages/mocker/src/browser/hints.ts:86
const importer = getImporter('mock')
_mocker().queueMock(
path,
importer,
typeof factory === 'function'
? () =>
factory(() =>
_mocker().importActual(
path,
importer,
),
)
: factory,
)
},
unmock(path: string | Promise<unknown>): void {
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?: ModuleMockOptions | ModuleMockFactoryWithHelper): void {
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 d568f8ce37)
Solutions
- Pass a string literal module path: vi.unmock('./logger').
- Ensure any variable passed is a string at runtime.
Example fix
// before
vi.unmock(config.loggerPath)
// after
vi.unmock('./logger') Defensive patterns
Strategy: type-guard
Validate before calling
function unmockIfString(path: unknown) {
if (typeof path !== 'string') {
throw new TypeError(`vi.unmock requires a string path, got ${typeof path}`)
}
vi.unmock(path)
} Type guard
function isModulePath(path: unknown): path is string {
return typeof path === 'string' && path.length > 0
} Prevention
- Pass a string literal module specifier to vi.unmock.
- Keep path variables strongly typed as string.
When it happens
Trigger: Calling vi.unmock(undefined), vi.unmock(someVar) with a non-string variable, or vi.unmock(import('./x')) that was not rewritten.
Common situations: Calling unmock with the wrong argument type after refactoring; passing an object/number accidentally; dynamic import not rewritten by transform.
Related errors
- vi.mock() expects a string path, but received a ${typeof pat
- vi.doMock() expects a string path, but received a ${typeof p
- vi.doUnmock() expects a string path, but received a ${typeof
- vi.hoisted() expects a function, but received a ${typeof fac
- toHaveFormValues must be called on a form or a fieldset, ins
AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03).
Data as JSON: /data/errors/2c229f575a556f5c.json.
Report an issue: GitHub.