{"record":{"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":"exception","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/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/registry.ts#L105-L141","documentation":"Thrown by MockRegistry.register when a mock of type 'redirect' is registered but factoryOrRedirect is not a string. Redirect mocks point one module at another module path, so the target must be a string path. This guard rejects functions, objects, or undefined for the redirect branch.","triggerScenarios":"Calling vi.mock('a', () => import('b')) when the system expects a redirect string; or registry.register({ type: 'redirect', ... }, nonString). Also when vi.mock's second argument is a factory but the internal type was resolved as redirect.","commonSituations":"Mixing up the redirect API (which takes a module path string) with the manual factory API (which takes a function); passing an object or undefined where a redirect path string is required; misconfigured mock redirection tooling.","solutions":[],"exampleFix":"// before\nregistry.register({ type: 'redirect', raw: 'a', id: 'a', url: 'a' }, () => 'b')\n// after\nregistry.register({ type: 'redirect', raw: 'a', id: 'a', url: 'a' }, 'b')","handlingStrategy":"validation","validationCode":"// Validate redirect target is a string path before registering\nif (typeof target !== 'string' || !target.trim()) {\n  throw new Error('redirect target must be a non-empty module path string')\n}\nregistry.register({ type: 'redirect', raw, id, url }, target)","typeGuard":"function isRedirectTarget(v: unknown): v is string { return typeof v === 'string' && v.length > 0 }","tryCatchPattern":null,"preventionTips":["Reserve redirect mocks for module-path strings only","Do not conflate the redirect API with the factory API","Unit-test register() inputs at the boundary"],"tags":["vitest","mocker","vi-mock","redirect"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}