{"record":{"id":"aa5c8dbf1cf053ac","repo":"vitest-dev/vitest","slug":"vitest-cannot-register-a-mock-that-is-already-de","errorCode":null,"errorMessage":"[vitest] Cannot register a mock that is already defined. Expected a JSON representation from `MockedModule.toJSON`, instead got \"${event.type}\". Use \"registry.add()\" to update a mock instead.","messagePattern":"\\[vitest\\] Cannot register a mock that is already defined\\. Expected a JSON representation from `MockedModule\\.toJSON`, instead got \"(.+?)\"\\. Use \"registry\\.add\\(\\)\" to update a mock instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/registry.ts","lineNumber":65,"sourceCode":"  ): AutospiedModule\n  public register(\n    typeOrEvent: MockedModuleType | MockedModuleSerialized,\n    raw?: string,\n    id?: string,\n    url?: string,\n    factoryOrRedirect?: string | (() => any),\n  ): MockedModule {\n    const type = typeof typeOrEvent === 'object' ? typeOrEvent.type : typeOrEvent\n\n    if (typeof typeOrEvent === 'object') {\n      const event = typeOrEvent\n      if (\n        event instanceof AutomockedModule\n        || event instanceof AutospiedModule\n        || event instanceof ManualMockedModule\n        || event instanceof RedirectedModule\n      ) {\n        throw new TypeError(\n          `[vitest] Cannot register a mock that is already defined. `\n          + `Expected a JSON representation from \\`MockedModule.toJSON\\`, instead got \"${event.type}\". `\n          + `Use \"registry.add()\" to update a mock instead.`,\n        )\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)","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/registry.ts#L47-L83","documentation":"Thrown as a `TypeError` by `MockerRegistry.register` when the object-form argument is an instance of one of the concrete mock classes (`AutomockedModule`, `AutospiedModule`, `ManualMockedModule`, `RedirectedModule`) rather than a plain JSON-serializable object. `register` expects the output of `MockedModule.toJSON()`; to store an already-constructed instance the caller must use `registry.add()` instead.","triggerScenarios":"Calling `registry.register(mockInstance)` with a constructed `MockedModule` instance instead of calling `registry.add(mockInstance)` or passing `mockInstance.toJSON()`. This is an internal API misuse — end users do not call `register` directly.","commonSituations":"Forking Vitest or writing a custom mocker that constructs module instances and passes them to `register` instead of `add`; refactoring that confuses the object-JSON overload with the instance-overload.","solutions":["Use `registry.add(mockInstance)` when you already have a constructed `MockedModule`.","Pass `mockInstance.toJSON()` to `register` if you need the JSON-deserialization path.","Switch to the multi-argument overload (`register(type, raw, id, url, ...)`) when constructing from primitives."],"exampleFix":"// before — passing an instance to register\nconst mock = new ManualMockedModule(raw, id, url, factory)\nregistry.register(mock)\n// after — use add for instances\nregistry.add(mock)","handlingStrategy":"type-guard","validationCode":"// Distinguish instances from JSON payloads before calling register vs add.\nimport { AutomockedModule, AutospiedModule, ManualMockedModule, RedirectedModule } from '@vitest/mocker'\nfunction isMockedModuleInstance(value: unknown): boolean {\n  return value instanceof AutomockedModule\n    || value instanceof AutospiedModule\n    || value instanceof ManualMockedModule\n    || value instanceof RedirectedModule\n}\nif (isMockedModuleInstance(payload)) {\n  registry.add(payload)\n} else {\n  registry.register(payload as any)\n}","typeGuard":"import type { MockedModule } from '@vitest/mocker'\nfunction isMockedModule(value: unknown): value is MockedModule {\n  return value instanceof AutomockedModule\n    || value instanceof AutospiedModule\n    || value instanceof ManualMockedModule\n    || value instanceof RedirectedModule\n}","tryCatchPattern":null,"preventionTips":["Use registry.add() for constructed instances; reserve register() for JSON payloads.","Add an internal wrapper that routes instances to add() and JSON to register()."],"tags":["registry","mock-registry","internal-api","typeerror"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}