{"record":{"id":"0ee21c232fea4465","repo":"jestjs/jest","slug":"cannot-replace-the-string-propertykey-proper","errorCode":null,"errorMessage":"Cannot replace the `${String(propertyKey)}` property because it has a getter. Use `jest.spyOn(object, '${String(propertyKey)}', 'get').mockReturnValue(value)` instead.","messagePattern":"Cannot replace the `(.+?)` property because it has a getter\\. Use `jest\\.spyOn\\(object, '(.+?)', 'get'\\)\\.mockReturnValue\\(value\\)` instead\\.","errorType":"validation","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/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/jest-mock/src/index.ts#L1472-L1508","documentation":"Thrown by jest.replaceProperty when the descriptor has a getter. Replacing a getter-backed value would silently discard the getter semantics, so Jest refuses and points you to spyOn with accessType 'get', which correctly wraps the getter.","triggerScenarios":"jest.replaceProperty(obj,'x',v) where x is declared with Object.defineProperty({get:...}) or a class accessor property.","commonSituations":"Class getter properties; config objects with computed getters; framework objects exposing derived values.","solutions":["Use jest.spyOn(obj,'x','get').mockReturnValue(value) to override the getter's return.","If you need to fully swap the property, redefine it without a getter first (only if configurable).","Prefer module-level jest.mock for source-owned code."],"exampleFix":"// before\njest.replaceProperty(obj, 'version', '2.0'); // version has a getter\n// after\njest.spyOn(obj, 'version', 'get').mockReturnValue('2.0');","handlingStrategy":"validation","validationCode":"const d = Object.getOwnPropertyDescriptor(obj, propertyKey);\nif (d?.get) {\n  jest.spyOn(obj, propertyKey, 'get').mockReturnValue(value);\n} else {\n  jest.replaceProperty(obj, propertyKey, value);\n}","typeGuard":"const hasGetter = (o: object, k: PropertyKey): boolean =>\n  Object.getOwnPropertyDescriptor(o, k)?.get !== undefined;","tryCatchPattern":null,"preventionTips":["Check the descriptor's get/set before choosing replaceProperty.","Use spyOn(obj, prop, 'get') for getter-backed values.","Centralise property-mocking decisions in a helper."],"tags":["mock","replace-property","accessor","getter"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}