jestjs/jest · error · Error
${methodName}() method does not exist
Error message
${methodName}() method does not exist What it means
Thrown by `SpyRegistry.spyOn` in spyRegistry.ts:100 when `obj[methodName] === void 0`. The host object exists but has no property by that name, so there is nothing to replace with a spy.
Source
Thrown at packages/jest-jasmine2/src/jasmine/spyRegistry.ts:100
this.spyOn = (obj, methodName, accessType) => {
if (accessType) {
return this._spyOnProperty(obj, methodName, accessType);
}
if (obj === void 0) {
throw new Error(
getErrorMsg(
`could not find an object to spy upon for ${methodName}()`,
),
);
}
if (methodName === void 0) {
throw new Error(getErrorMsg('No method name supplied'));
}
if (obj[methodName] === void 0) {
throw new Error(getErrorMsg(`${methodName}() method does not exist`));
}
if (obj[methodName] && isSpy(obj[methodName])) {
if (this.respy) {
return obj[methodName];
} else {
throw new Error(
getErrorMsg(`${methodName} has already been spied upon`),
);
}
}
let descriptor;
try {
descriptor = Object.getOwnPropertyDescriptor(obj, methodName);
} catch {
// IE 8 doesn't support `definePropery` on non-DOM nodes
}View on GitHub (pinned to f49721c78e)
Solutions
- Verify the method name exists on the object: `console.log(typeof obj.methodName)`.
- If the method is on the prototype, spy on the prototype: `spyOn(MyClass.prototype, 'method')`.
- After a dependency upgrade, grep the new version for the method name to catch renames.
Example fix
// before spyOn(userService, 'fetchUser') // after spyOn(userService, 'getUser')
Defensive patterns
Strategy: validation
Validate before calling
if (typeof obj[methodName] !== 'function') { throw new Error(`${methodName} is not a method on obj`); }
spyOn(obj, methodName); Type guard
const hasMethod = (o: Record<string, any>, m: string): boolean => typeof o?.[m] === 'function';
Prevention
- After dependency upgrades, grep the test suite for spyOn calls and verify names.
- Spy on the prototype for inherited methods.
When it happens
Trigger: Calling `spyOn(userService, 'fetchUser')` when the method is actually named `getUser`; spying on an instance method that only exists on the prototype and the instance does not expose it; the method was renamed/removed in a library upgrade.
Common situations: Renaming a source method without updating tests; mocking a method that lives on a different object (class vs instance); version mismatch where the dependency dropped or renamed the API.
Related errors
- could not find an object to spy upon for ${methodName}()
- No method name supplied
- ${methodName} has already been spied upon
- ${methodName} is not declared writable or has no setter
- Spies must be created in a before function or a spec
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/ef7c631181a6ccec.json.
Report an issue: GitHub.