{"id":"e4fa846b6ed10753","repo":"jestjs/jest","slug":"cannot-replace-the-propertykey-property-becau-e4fa84","errorCode":null,"errorMessage":"Cannot replace the `${propertyKey}` property because it has a setter. Use `jest.spyOn(object, '${propertyKey}', 'set').mockReturnValue(value)` instead.","messagePattern":"Cannot replace the `(.+?)` property because it has a setter\\. Use `jest\\.spyOn\\(object, '(.+?)', 'set'\\)\\.mockReturnValue\\(value\\)` instead\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-mock/src/index.ts","lineNumber":1500,"sourceCode":"    }\n    if (!descriptor.configurable) {\n      throw new Error(\n        `Property \\`${String(propertyKey)}\\` is not declared configurable`,\n      );\n    }\n\n    if (descriptor.get !== undefined) {\n      throw new Error(\n        `Cannot replace the \\`${String(\n          propertyKey,\n        )}\\` property because it has a getter. Use \\`jest.spyOn(object, '${String(\n          propertyKey,\n        )}', 'get').mockReturnValue(value)\\` instead.`,\n      );\n    }\n\n    if (descriptor.set !== undefined) {\n      throw new Error(\n        `Cannot replace the \\`${String(\n          propertyKey,\n        )}\\` property because it has a setter. Use \\`jest.spyOn(object, '${String(\n          propertyKey,\n        )}', 'set').mockReturnValue(value)\\` instead.`,\n      );\n    }\n\n    if (typeof descriptor.value === 'function') {\n      throw new TypeError(\n        `Cannot replace the \\`${String(\n          propertyKey,\n        )}\\` property because it is a function. Use \\`jest.spyOn(object, '${String(\n          propertyKey,\n        )}')\\` instead.`,\n      );\n    }\n","sourceCodeStart":1482,"sourceCodeEnd":1518,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-mock/src/index.ts#L1482-L1518","documentation":"Thrown by jest.replaceProperty when the descriptor has a setter (descriptor.set !== undefined). The same rationale as error 155: replaceProperty cannot replace an accessor-backed property, and the message directs you to jest.spyOn(object, 'key', 'set').mockReturnValue(value).","triggerScenarios":"jest.replaceProperty(obj, 'value', v) where value is backed by a set accessor (write-only or get/set pair); targeting a property of a class that declared set value(x){...}.","commonSituations":"Write-only credential setters; classes that validate on assignment via a setter; reactive frameworks that intercept assignment.","solutions":["Use jest.spyOn(object, 'key', 'set') to intercept assignments and mockReturnValue if needed.","If both get and set exist, decide which direction you need and spy on that one (or both).","If you truly must replace the whole property, redefine it with Object.defineProperty first (if configurable)."],"exampleFix":"// before\njest.replaceProperty(form, 'email', 'a@b.com'); // email has a setter\n// after\nconst spy = jest.spyOn(form, 'email', 'set');\nform.email = 'a@b.com';\nexpect(spy).toHaveBeenCalledWith('a@b.com');","handlingStrategy":"validation","validationCode":"const d = Object.getOwnPropertyDescriptor(obj, key) ?? Object.getOwnPropertyDescriptor(Object.getPrototypeOf(obj), key);\nif (d && d.set) {\n  const spy = jest.spyOn(obj, key, 'set');\n} else {\n  jest.replaceProperty(obj, key, value);\n}","typeGuard":"const isSetterProp = (o: object, k: PropertyKey) => {\n  let d = Object.getOwnPropertyDescriptor(o, k);\n  let p = Object.getPrototypeOf(o);\n  while (!d && p) { d = Object.getOwnPropertyDescriptor(p, k); p = Object.getPrototypeOf(p); }\n  return !!(d && typeof d.set === 'function');\n};\n\nif (isSetterProp(obj, key)) {\n  jest.spyOn(obj, key, 'set');\n} else {\n  jest.replaceProperty(obj, key, value);\n}","tryCatchPattern":null,"preventionTips":["Check the descriptor for 'set' before calling replaceProperty on assignable properties.","Use spyOn(..., 'set') to validate or intercept assignments to setter-backed properties.","Remember write-only properties have only 'set'."],"tags":["jest-mock","replaceproperty","accessor","setter"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}