{"id":"80671c44dfc3357a","repo":"vitest-dev/vitest","slug":"unknown-mock-type-event-as-any-type","errorCode":null,"errorMessage":"Unknown mock type: ${(event as any).type}","messagePattern":"Unknown mock type: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/registry.ts","lineNumber":90,"sourceCode":"        const module = AutomockedModule.fromJSON(event)\n        this.add(module)\n        return module\n      }\n      else if (event.type === 'autospy') {\n        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.')","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/registry.ts#L72-L108","documentation":"When rehydrating a serialized mock, the registry dispatches on the object's `type` field and only recognizes `automock`, `autospy`, `redirect`, and `manual`. Any other value — typo, `undefined`, or a future/extension type — falls through to this error.","triggerScenarios":"Calling `registry.register({ type: 'manualMock', ... })` (wrong casing), `{ type: undefined }`, or a hand-built payload with an invented type string.","commonSituations":"Hand-crafting mock JSON; schema drift between Vitest versions; corrupted IPC payload; third-party tools emitting non-standard mock objects.","solutions":["Set `type` to one of the supported values: `'automock'`, `'autospy'`, `'redirect'`, or `'manual'`.","If you extended the mock types, update `register()` to handle the new branch instead of relying on the serialized path.","Validate the payload shape before calling register (see defense)."],"exampleFix":"// before\nregistry.register({ type: 'Manual', raw, id, url })\n\n// after\nregistry.register({ type: 'manual', raw, id, url })","handlingStrategy":"validation","validationCode":"const VALID_TYPES = new Set(['automock', 'autospy', 'redirect', 'manual'])\n\nfunction safeRegister(registry: any, dto: { type: string }) {\n  if (!VALID_TYPES.has(dto?.type)) {\n    throw new Error(`Unsupported mock type '${dto?.type}'. Valid: ${[...VALID_TYPES].join(', ')}`)\n  }\n  registry.register(dto)\n}","typeGuard":"function isMockedModuleSerialized(v: unknown): v is { type: 'automock' | 'autospy' | 'redirect' | 'manual' } {\n  return typeof v === 'object' && v !== null\n    && ['automock', 'autospy', 'redirect', 'manual'].includes((v as any).type)\n}","tryCatchPattern":null,"preventionTips":["Constrain generated `type` values to the `MockedModuleType` union via TypeScript.","Validate inbound IPC payloads against the whitelist before registering.","Add a schema test that every serialized mock carries a valid type."],"tags":["mocking","registry","validation","serialization"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}