{"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":"validation","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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/registry.ts#L85-L121","documentation":"The positional register overload requires `id` — the unique internal mock identifier — to be a string, since the registry maintains a secondary index (`registryById`) keyed by `id`. A non-string `id` would silently corrupt lookups.","triggerScenarios":"Calling `registry.register('automock', raw, undefined, url)` or passing a number/object as the id.","commonSituations":"Generating ids programmatically and forgetting to stringify; misordered arguments; refactor that lost the id.","solutions":["Provide a unique string id (commonly the resolved specifier or a generated UUID).","Check argument order: `(type, raw, id, url, factoryOrRedirect)`.","If ids are generated, ensure the generator returns a string."],"exampleFix":"// before\nregistry.register('automock', raw, 42, url)\n\n// after\nregistry.register('automock', raw, '/abs/mod', url)","handlingStrategy":"validation","validationCode":"function registerWithId(registry: any, type: string, raw: string, id: unknown, url: string, extra?: unknown) {\n  if (typeof id !== 'string' || id.length === 0) {\n    throw new TypeError(`register('${type}'): 'id' must be a non-empty string, got ${typeof id}`)\n  }\n  registry.register(type, raw, id, url, extra)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Generate ids from a single string-returning helper (e.g. `pathToFileURL(resolved).href`).","Reject empty/whitespace ids at the boundary.","Type wrapper params as `(type: MockedModuleType, raw: string, id: string, ...)`."],"tags":["mocking","registry","validation"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}