{"record":{"id":"6f53ba28ed017703","repo":"vitest-dev/vitest","slug":"vitest-mocks-require-an-id-string","errorCode":null,"errorMessage":"[vitest] Mocks require an id string.","messagePattern":"\\[vitest\\] Mocks require an id string\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/registry.ts","lineNumber":103,"sourceCode":"      }\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    }\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') {","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/registry.ts#L85-L121","documentation":"Thrown as a `TypeError` by `MockerRegistry.register` (multi-argument overload) when the `id` argument is not a string. The `id` is the resolved module id used as a secondary registry key (via `registryById`) and for invalidation; it must be a string so the mock can be tracked and cleared across test isolation boundaries.","triggerScenarios":"Calling `register(type, raw, id, url, ...)` with `id` set to `undefined` or a non-string — typically because the RPC resolution did not return a `resolvedId`, or the caller passed arguments out of order.","commonSituations":"An RPC `resolveMock`/`resolveId` response missing `resolvedId`; a forked runner that drops the id field; argument-order confusion against the `(type, raw, id, url)` signature.","solutions":["Ensure the RPC resolution returns a string `resolvedId` and that it is forwarded as the `id` argument.","Confirm the argument order matches `(type, raw, id, url, factoryOrRedirect)`.","If the id is genuinely absent, fall back to the resolved file path before calling `register`."],"exampleFix":"// before — id missing from the RPC result\nconst { resolvedUrl } = await rpc.resolveMock(...)\nregistry.register('automock', raw, undefined, resolvedUrl)\n// after — forward resolvedId\nconst { resolvedId, resolvedUrl } = await rpc.resolveMock(...)\nregistry.register('automock', raw, resolvedId, resolvedUrl)","handlingStrategy":"validation","validationCode":"// Validate id is a string before calling register.\nfunction registerSafe(registry: MockerRegistry, type: any, raw: string, id: unknown, url: string, extra?: any) {\n  if (typeof id !== 'string') {\n    throw new TypeError('id must be a string')\n  }\n  return registry.register(type, raw, id, url, extra)\n}","typeGuard":"function isIdString(value: unknown): value is string {\n  return typeof value === 'string'\n}","tryCatchPattern":null,"preventionTips":["Ensure RPC resolveMock returns a string resolvedId and forward it as id.","Respect the (type, raw, id, url) argument order.","Add unit tests covering the full argument-shape matrix for register()."],"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"}