{"id":"2c9cc70848757eaa","repo":"jestjs/jest","slug":"could-not-find-an-object-to-spy-upon-for-propert","errorCode":null,"errorMessage":"could not find an object to spy upon for ${propertyName}","messagePattern":"could not find an object to spy upon for (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-jasmine2/src/jasmine/spyRegistry.ts","lineNumber":155,"sourceCode":"        restoreStrategy = function () {\n          if (!delete obj[methodName]) {\n            obj[methodName] = originalMethod;\n          }\n        };\n      }\n\n      currentSpies().push({\n        restoreObjectToOriginalState: restoreStrategy,\n      } as Spy);\n\n      obj[methodName] = spiedMethod;\n\n      return spiedMethod;\n    };\n\n    this._spyOnProperty = function (obj, propertyName, accessType = 'get') {\n      if (!obj) {\n        throw new Error(\n          getErrorMsg(\n            `could not find an object to spy upon for ${propertyName}`,\n          ),\n        );\n      }\n\n      if (!propertyName) {\n        throw new Error(getErrorMsg('No property name supplied'));\n      }\n\n      let descriptor: PropertyDescriptor | undefined;\n      try {\n        descriptor = Object.getOwnPropertyDescriptor(obj, propertyName);\n      } catch {\n        // IE 8 doesn't support `definePropery` on non-DOM nodes\n      }\n\n      if (!descriptor) {","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-jasmine2/src/jasmine/spyRegistry.ts#L137-L173","documentation":"Thrown by `SpyRegistry._spyOnProperty` in spyRegistry.ts:155 when the target object is falsy (`!obj`). This is the property-access variant of `spyOn` (used by `spyOnProperty`), reached when an `accessType` argument is supplied. The registry needs a real host object.","triggerScenarios":"Calling `spyOnProperty(undefined, 'x', 'get')`, `spyOnProperty(null, 'x', 'get')`, or `jest.spyOn(obj, 'x', 'get')` where `obj` resolved to undefined at runtime.","commonSituations":"Importing a named binding that does not exist under ESM/CJS interop; spying before the object is initialized; refactor leaving a stale reference.","solutions":["Confirm the target object is defined where `spyOnProperty` runs.","Move the spy into a `beforeEach`/test body that runs after the object is created.","If mocking a module export, verify the import path and named export."],"exampleFix":"// before\nspyOnProperty(undefined, 'host', 'get')\n// after\nconst svc = require('../svc');\nspyOnProperty(svc, 'host', 'get')","handlingStrategy":"type-guard","validationCode":"if (!obj) { throw new Error('spyOnProperty needs a target object'); }\nspyOnProperty(obj, 'p', 'get');","typeGuard":"const isSpyTarget = (o: unknown): o is Record<string, any> =>\n  o != null && (typeof o === 'object' || typeof o === 'function');","tryCatchPattern":null,"preventionTips":["Verify the host object is initialized before spying.","Prefer spying on a stable prototype over a transient instance."],"tags":["jasmine2","spy","spy-on-property","undefined"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}