{"record":{"id":"59e6771cdcd2b4e1","repo":"vitest-dev/vitest","slug":"vitest-mocks-require-a-raw-string","errorCode":null,"errorMessage":"[vitest] Mocks require a raw string.","messagePattern":"\\[vitest\\] Mocks require a raw string\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/registry.ts","lineNumber":95,"sourceCode":"        const module = AutospiedModule.fromJSON(event)\n        this.add(module)\n        return module\n      }\n      else if (event.type === 'redirect') {\n        const module = RedirectedModule.fromJSON(event)\n        this.add(module)\n        return module\n      }\n      else if (event.type === 'manual') {\n        throw new Error(`Cannot set serialized manual mock. Define a factory function manually with \\`ManualMockedModule.fromJSON()\\`.`)\n      }\n      else {\n        throw new Error(`Unknown mock type: ${(event as any).type}`)\n      }\n    }\n\n    if (typeof raw !== 'string') {\n      throw new TypeError('[vitest] Mocks require a raw string.')\n    }\n\n    if (typeof url !== 'string') {\n      throw new TypeError('[vitest] Mocks require a url string.')\n    }\n\n    if (typeof id !== 'string') {\n      throw new TypeError('[vitest] Mocks require an id string.')\n    }\n\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    }","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/registry.ts#L77-L113","documentation":"Thrown as a `TypeError` by `MockerRegistry.register` (multi-argument overload) when the `raw` argument is not a string. `raw` is the original module specifier as written by the user (e.g., the path passed to `vi.mock`); it is used in error messages and as a human-readable identifier, so it must be a string.","triggerScenarios":"Calling the multi-arg `register(type, raw, id, url, ...)` with `raw` set to `undefined`, a number, or any non-string — typically an internal caller that forgot to forward the specifier or passed it positionally out of order.","commonSituations":"Internal mocker code that destructures the RPC result incorrectly; a forked runner that swaps argument order; passing `undefined` when the module has no original specifier.","solutions":["Ensure the original module specifier string is forwarded as the `raw` argument.","Double-check argument order against the `register` overloads — the signature is `(type, raw, id, url, factoryOrRedirect)`.","Default `raw` to the id or a placeholder string if the original specifier is genuinely unavailable."],"exampleFix":"// before — raw omitted / wrong position\nregistry.register('automock', undefined, id, url)\n// after — forward the specifier\nregistry.register('automock', rawId, id, url)","handlingStrategy":"validation","validationCode":"// Validate raw is a string before calling register.\nfunction registerSafe(registry: MockerRegistry, type: any, raw: unknown, id: string, url: string, extra?: any) {\n  if (typeof raw !== 'string') {\n    throw new TypeError('raw must be a string module specifier')\n  }\n  return registry.register(type, raw, id, url, extra)\n}","typeGuard":"function isRawSpecifier(value: unknown): value is string {\n  return typeof value === 'string'\n}","tryCatchPattern":null,"preventionTips":["Forward the original module specifier as the raw argument in all internal callers.","Match the (type, raw, id, url) argument order exactly.","Add unit tests for register() argument validation."],"tags":["registry","mock-registry","internal-api","validation","typeerror"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}