{"record":{"id":"3a97f298e5989c05","repo":"vitest-dev/vitest","slug":"cannot-set-serialized-manual-mock-define-a-factor","errorCode":null,"errorMessage":"Cannot set serialized manual mock. Define a factory function manually with `ManualMockedModule.fromJSON()`.","messagePattern":"Cannot set serialized manual mock\\. Define a factory function manually with `ManualMockedModule\\.fromJSON\\(\\)`\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/registry.ts","lineNumber":87,"sourceCode":"        )\n      }\n      if (event.type === 'automock') {\n        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","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/registry.ts#L69-L105","documentation":"Thrown by `MockerRegistry.register` (object-form) when `event.type === 'manual'`. Manual mocks carry a factory function, which cannot be serialized to JSON, so there is no way to reconstruct a `ManualMockedModule` from a plain object alone. The caller must instead use the multi-argument overload that supplies the factory function directly.","triggerScenarios":"A serialized manual-mock event arrives at `register` via the object path — e.g., an RPC message or IPC payload with `{ type: 'manual', ... }` and no accompanying factory. This happens when browser↔node mock coordination tries to round-trip a manual mock through JSON without reattaching the factory.","commonSituations":"Forking the mocker transport; a Vitest internal regression that routes manual mocks through the serialization path; custom interceptors that forward events verbatim.","solutions":["Use the multi-argument overload `register('manual', raw, id, url, factory)` to supply the factory function.","If deserializing, call `ManualMockedModule.fromJSON(data, factory)` and then `registry.add(module)`.","Ensure the transport layer reattaches the factory before registration rather than sending a bare JSON payload."],"exampleFix":"// before — object path cannot carry a factory\nregistry.register({ type: 'manual', raw, id, url })\n// after — multi-arg overload with factory\nregistry.register('manual', raw, id, url, () => ({ fn: vi.fn() }))","handlingStrategy":"validation","validationCode":"// Never send a manual mock through the object path; route it to the multi-arg overload.\nfunction registerMock(registry: MockerRegistry, event: any) {\n  if (event.type === 'manual') {\n    throw new Error('Manual mocks must use the multi-arg register overload with a factory.')\n  }\n  return registry.register(event)\n}","typeGuard":"function isManualSerializedEvent(event: unknown): boolean {\n  return typeof event === 'object' && event !== null && (event as any).type === 'manual'\n}","tryCatchPattern":null,"preventionTips":["Always pass manual mocks through the multi-arg register overload with a factory.","If deserializing from JSON, reattach the factory via ManualMockedModule.fromJSON and use add().","Keep the transport layer from round-tripping manual mocks as bare JSON."],"tags":["registry","mock-registry","manual-mock","serialization"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}