{"id":"1d7aed5ee9253d31","repo":"jestjs/jest","slug":"cannot-replace-the-propertykey-property-becau","errorCode":null,"errorMessage":"Cannot replace the `${propertyKey}` property because it has a getter. Use `jest.spyOn(object, '${propertyKey}', 'get').mockReturnValue(value)` instead.","messagePattern":"Cannot replace the `(.+?)` property because it has a getter\\. Use `jest\\.spyOn\\(object, '(.+?)', 'get'\\)\\.mockReturnValue\\(value\\)` instead\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-mock/src/index.ts","lineNumber":1490,"sourceCode":"    while (!descriptor && proto !== null) {\n      descriptor = Object.getOwnPropertyDescriptor(proto, propertyKey);\n      proto = Object.getPrototypeOf(proto);\n    }\n    if (!descriptor) {\n      throw new Error(\n        `Property \\`${String(\n          propertyKey,\n        )}\\` does not exist in the provided object`,\n      );\n    }\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","sourceCodeStart":1472,"sourceCodeEnd":1508,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-mock/src/index.ts#L1472-L1508","documentation":"Thrown by jest.replaceProperty when the descriptor has a getter (descriptor.get !== undefined). replaceProperty only handles data properties; replacing an accessor's value would silently lose the getter. The message tells you the correct tool: jest.spyOn(object, 'key', 'get').mockReturnValue(value).","triggerScenarios":"jest.replaceProperty(store, 'count', 5) where count is defined with a get accessor; replacing a class field declared with get; a library that exposes computed read-only properties.","commonSituations":"Modern class syntax using get x() {...} that looks like a data field; refactoring a value property into a computed getter without updating tests; libraries (MobX, Vue) that back properties with getters.","solutions":["Follow the message: const spy = jest.spyOn(object, 'key', 'get'); spy.mockReturnValue(value); and spy.mockRestore() after.","If you need to truly replace the descriptor, redefine it first with Object.defineProperty to remove the getter, then use replaceProperty (only if configurable).","Prefer the spyOn path — it restores cleanly."],"exampleFix":"// before\njest.replaceProperty(store, 'count', 5); // count has a getter\n// after\nconst spy = jest.spyOn(store, 'count', 'get').mockReturnValue(5);\n// ... in afterEach: spy.mockRestore();","handlingStrategy":"validation","validationCode":"const d = Object.getOwnPropertyDescriptor(obj, key) ?? Object.getOwnPropertyDescriptor(Object.getPrototypeOf(obj), key);\nif (d && d.get) {\n  // use spyOn 'get' instead\n  const spy = jest.spyOn(obj, key, 'get').mockReturnValue(value);\n} else {\n  jest.replaceProperty(obj, key, value);\n}","typeGuard":"const isGetterProp = (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.get === 'function');\n};\n\nif (isGetterProp(obj, key)) {\n  jest.spyOn(obj, key, 'get').mockReturnValue(value);\n} else {\n  jest.replaceProperty(obj, key, value);\n}","tryCatchPattern":null,"preventionTips":["Inspect the descriptor: if it has 'get', use spyOn(..., 'get').mockReturnValue.","Treat computed/getter-backed properties as accessors, not data.","Watch for reactive libraries (MobX/Vue) that back properties with getters."],"tags":["jest-mock","replaceproperty","accessor","getter"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}