{"id":"2bd62ecf300cc23f","repo":"vitest-dev/vitest","slug":"vitest-redirect-mocks-require-a-redirect-string","errorCode":null,"errorMessage":"[vitest] Redirect mocks require a redirect string.","messagePattern":"\\[vitest\\] Redirect mocks require a redirect string\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/registry.ts","lineNumber":123,"sourceCode":"\n    if (type === 'manual') {\n      if (typeof factoryOrRedirect !== 'function') {\n        throw new TypeError('[vitest] Manual mocks require a factory function.')\n      }\n      const mock = new ManualMockedModule(raw, id, url, factoryOrRedirect)\n      this.add(mock)\n      return mock\n    }\n    else if (type === 'automock' || type === 'autospy') {\n      const mock = type === 'automock'\n        ? new AutomockedModule(raw, id, url)\n        : new AutospiedModule(raw, id, url)\n      this.add(mock)\n      return mock\n    }\n    else if (type === 'redirect') {\n      if (typeof factoryOrRedirect !== 'string') {\n        throw new TypeError('[vitest] Redirect mocks require a redirect string.')\n      }\n      const mock = new RedirectedModule(raw, id, url, factoryOrRedirect)\n      this.add(mock)\n      return mock\n    }\n    else {\n      throw new Error(`[vitest] Unknown mock type: ${type}`)\n    }\n  }\n\n  public delete(id: string): void {\n    this.registryByUrl.delete(id)\n  }\n\n  public deleteById(id: string): void {\n    this.registryById.delete(id)\n  }\n","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/registry.ts#L105-L141","documentation":"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.","triggerScenarios":"Calling `registry.register('redirect', raw, id, url)` (target omitted) or passing a non-string such as a function or number.","commonSituations":"Confusing the manual (factory) and redirect (string target) overloads; forgetting the target specifier; refactor dropping the argument.","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."],"exampleFix":"// before\nregistry.register('redirect', raw, id, url)\n\n// after\nregistry.register('redirect', raw, id, url, './__mocks__/fs')","handlingStrategy":"validation","validationCode":"function registerRedirect(registry: any, raw: string, id: string, url: string, target: unknown) {\n  if (typeof target !== 'string' || target.length === 0) {\n    throw new TypeError(`Redirect mocks require a non-empty target string, got ${typeof target}`)\n  }\n  registry.register('redirect', raw, id, url, target)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["mocking","registry","validation","redirect"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}