vitest-dev/vitest · error · TypeError
[vitest] Redirect mocks require a redirect string.
Error message
[vitest] Redirect mocks require a redirect string.
What it means
Redirect mocks point one module specifier at another real module; the 5th argument must be the redirect target specifier string. A non-string (or omitted) value means the registry cannot resolve where to redirect imports.
Source
Thrown at packages/mocker/src/registry.ts:123
if (type === 'manual') {
if (typeof factoryOrRedirect !== 'function') {
throw new TypeError('[vitest] Manual mocks require a factory function.')
}
const mock = new ManualMockedModule(raw, id, url, factoryOrRedirect)
this.add(mock)
return mock
}
else if (type === 'automock' || type === 'autospy') {
const mock = type === 'automock'
? new AutomockedModule(raw, id, url)
: new AutospiedModule(raw, id, url)
this.add(mock)
return mock
}
else if (type === 'redirect') {
if (typeof factoryOrRedirect !== 'string') {
throw new TypeError('[vitest] Redirect mocks require a redirect string.')
}
const mock = new RedirectedModule(raw, id, url, factoryOrRedirect)
this.add(mock)
return mock
}
else {
throw new Error(`[vitest] Unknown mock type: ${type}`)
}
}
public delete(id: string): void {
this.registryByUrl.delete(id)
}
public deleteById(id: string): void {
this.registryById.delete(id)
}
View on GitHub (pinned to d568f8ce37)
Solutions
- Pass the redirect target as a string: `registry.register('redirect', raw, id, url, './real-module')`.
- Remember the 5th arg is a function for `'manual'` but a string for `'redirect'`.
- Resolve the target specifier before registering.
Example fix
// before
registry.register('redirect', raw, id, url)
// after
registry.register('redirect', raw, id, url, './__mocks__/fs') Defensive patterns
Strategy: validation
Validate before calling
function registerRedirect(registry: any, raw: string, id: string, url: string, target: unknown) {
if (typeof target !== 'string' || target.length === 0) {
throw new TypeError(`Redirect mocks require a non-empty target string, got ${typeof target}`)
}
registry.register('redirect', raw, id, url, target)
} Prevention
- Resolve the redirect target to a specifier string before registering.
- Remember the 5th arg is a string for redirect but a function for manual.
- Add a type test covering the redirect overload signature.
When it happens
Trigger: Calling `registry.register('redirect', raw, id, url)` (target omitted) or passing a non-string such as a function or number.
Common situations: Confusing the manual (factory) and redirect (string target) overloads; forgetting the target specifier; refactor dropping the argument.
Related errors
- Unknown mock type: ${(event as any).type}
- [vitest] Mocks require a raw string.
- [vitest] Mocks require a url string.
- [vitest] Mocks require an id string.
- [vitest] Manual mocks require a factory function.
AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03).
Data as JSON: /data/errors/2bd62ecf300cc23f.json.
Report an issue: GitHub.