vitest-dev/vitest · error · TypeError
vi.doUnmock() expects a string path, but received a ${typeof
Error message
vi.doUnmock() expects a string path, but received a ${typeof path} What it means
Thrown at packages/mocker/src/browser/hints.ts:116-119 as a TypeError when vi.doUnmock() is called with a non-string first argument. vi.doUnmock is the non-hoisted counterpart of vi.unmock and needs a string specifier to remove a mock from the registry at call time.
Source
Thrown at packages/mocker/src/browser/hints.ts:117
const importer = getImporter('doMock')
_mocker().queueMock(
path,
importer,
typeof factory === 'function'
? () =>
factory(() =>
_mocker().importActual(
path,
importer,
),
)
: factory,
)
},
doUnmock(path: string | Promise<unknown>): void {
if (typeof path !== 'string') {
throw new TypeError(
`vi.doUnmock() expects a string path, but received a ${typeof path}`,
)
}
_mocker().queueUnmock(path, getImporter('doUnmock'))
},
async importActual<T = unknown>(path: string): Promise<T> {
return _mocker().importActual<T>(
path,
getImporter('importActual'),
)
},
async importMock<T>(path: string): Promise<MaybeMockedDeep<T>> {
return _mocker().importMock(path, getImporter('importMock'))
},
}
}View on GitHub (pinned to d568f8ce37)
Solutions
- Pass a string literal module path: vi.doUnmock('./logger').
- Validate any dynamic path is a string before calling.
Example fix
// before
vi.doUnmock(import('./logger'))
// after
vi.doUnmock('./logger') Defensive patterns
Strategy: type-guard
Validate before calling
function doUnmockIfString(path: unknown) {
if (typeof path !== 'string') {
throw new TypeError(`vi.doUnmock requires a string path, got ${typeof path}`)
}
vi.doUnmock(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.doUnmock.
- Validate dynamically computed paths are strings before calling.
When it happens
Trigger: Calling vi.doUnmock(undefined), vi.doUnmock(someVar) where someVar is not a string, or vi.doUnmock(import('./x')) that escaped transform rewriting.
Common situations: Refactoring unmock calls to doUnmock and dropping the path; passing a computed value that is not a string.
Related errors
- vi.mock() expects a string path, but received a ${typeof pat
- vi.unmock() expects a string path, but received a ${typeof p
- vi.doMock() expects a string path, but received a ${typeof p
- 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/0ede60c5c0032e65.json.
Report an issue: GitHub.