{"id":"2ad0c4aba2bc9753","repo":"jestjs/jest","slug":"property-methodkey-does-not-exist-in-the-prov","errorCode":null,"errorMessage":"Property `${methodKey}` does not exist in the provided object","messagePattern":"Property `(.+?)` does not exist in the provided object","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-mock/src/index.ts","lineNumber":1296,"sourceCode":"      (typeof object !== 'object' && typeof object !== 'function')\n    ) {\n      throw new Error(\n        `Cannot use spyOn on a primitive value; ${this._typeOf(object)} given`,\n      );\n    }\n\n    if (methodKey == null) {\n      throw new Error('No property name supplied');\n    }\n\n    if (accessType) {\n      return this._spyOnProperty(object, methodKey, accessType);\n    }\n\n    const original = object[methodKey];\n\n    if (!original) {\n      throw new Error(\n        `Property \\`${String(\n          methodKey,\n        )}\\` does not exist in the provided object`,\n      );\n    }\n\n    if (!this.isMockFunction(original)) {\n      if (typeof original !== 'function') {\n        throw new TypeError(\n          `Cannot spy on the \\`${String(\n            methodKey,\n          )}\\` property because it is not a function; ${this._typeOf(\n            original,\n          )} given instead.${\n            typeof original === 'object'\n              ? ''\n              : ` If you are trying to mock a property, use \\`jest.replaceProperty(object, '${String(\n                  methodKey,","sourceCodeStart":1278,"sourceCodeEnd":1314,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-mock/src/index.ts#L1278-L1314","documentation":"Thrown by jest.spyOn when object[methodKey] is falsy (the !original check). This means the property is undefined, null, 0, false, or '' at the moment spyOn reads it. Most commonly the property simply does not exist on the object or its prototype chain as a value (it may be an accessor, or the method is not implemented). Note this checks the runtime value, not the type signature — a method that exists in types but is undefined at runtime trips this.","triggerScenarios":"jest.spyOn(obj, 'nope') where obj has no 'nope'; spying on a method that lives on the prototype but the instance shadows it with undefined; spying before the module has populated the method (import order); method name typo (jest.spyOn(fs, 'readfile') instead of 'readFile').","commonSituations":"Case mismatch in the method name; tree-shaking removed the method; the method is a getter not a value (use the third arg 'get'); ESM live bindings where the export is not yet initialized when the spy runs.","solutions":["Confirm the property exists at runtime: console.log(typeof obj.methodName) and check spelling/casing.","If the method is an accessor, pass the access type: jest.spyOn(obj, 'prop', 'get').","If it is a non-function value you want to replace, use jest.replaceProperty(obj, 'prop', value).","Ensure the spy runs after the module under test has fully loaded its exports."],"exampleFix":"// before\njest.spyOn(fs.promises, 'readfile'); // wrong casing -> undefined\n// after\njest.spyOn(fs.promises, 'readFile');","handlingStrategy":"validation","validationCode":"if (!obj[methodKey]) {\n  throw new Error(`Property '${String(methodKey)}' not found on object before spying`);\n}\njest.spyOn(obj, methodKey);","typeGuard":"function hasMethod<T extends object, K extends keyof T>(\n  obj: T,\n  k: K,\n): obj is T & Record<K, Function> {\n  return typeof obj[k] === 'function';\n}\n\nif (hasMethod(service, 'send')) {\n  jest.spyOn(service, 'send');\n}","tryCatchPattern":null,"preventionTips":["Double-check method name casing and spelling against the source.","If the property is an accessor, pass the third 'get'/'set' argument.","Ensure the spy runs after the module fully loads its exports."],"tags":["jest-mock","spyon","missing-property","typo"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}