jestjs/jest · error · Error
No property name supplied
Error message
No property name supplied
What it means
Thrown by jest.spyOn when the second argument (methodKey) is null or undefined. The guard is simply methodKey == null. Without a property name the mocker has nothing to look up on the descriptor, so it fails fast rather than silently installing a spy on a random slot.
Source
Thrown at packages/jest-mock/src/index.ts:1286
methodKey: K,
): V extends ClassLike | FunctionLike ? Spied<V> : never;
spyOn<T extends object>(
object: T,
methodKey: keyof T,
accessType?: 'get' | 'set',
): MockInstance {
if (
object == null ||
(typeof object !== 'object' && typeof object !== 'function')
) {
throw new Error(
`Cannot use spyOn on a primitive value; ${this._typeOf(object)} given`,
);
}
if (methodKey == null) {
throw new Error('No property name supplied');
}
if (accessType) {
return this._spyOnProperty(object, methodKey, accessType);
}
const original = object[methodKey];
if (!original) {
throw new Error(
`Property \`${String(
methodKey,
)}\` does not exist in the provided object`,
);
}
if (!this.isMockFunction(original)) {
if (typeof original !== 'function') {View on GitHub (pinned to f49721c78e)
Solutions
- Provide the literal property name: jest.spyOn(obj, 'methodName').
- If the name is dynamic, validate it is a non-null string before calling: if (!key) throw new Error('key missing').
- Check that the variable holding the method key was actually assigned — log it before the call.
Example fix
// before jest.spyOn(logger); // no method name // after jest.spyOn(logger, 'info');
Defensive patterns
Strategy: validation
Validate before calling
if (methodKey == null) {
throw new Error('spyOn requires a method name as the second argument');
}
jest.spyOn(obj, methodKey); Type guard
const hasMethodName = (k: unknown): k is string | number | symbol =>
k != null;
if (hasMethodName(methodKey)) {
jest.spyOn(obj, methodKey);
} Prevention
- Always pass the method name as a string literal when possible.
- For dynamic keys, assert non-null before the call.
- Enable TypeScript's checks so missing args fail at compile time.
When it happens
Trigger: jest.spyOn(obj); (missing second arg); jest.spyOn(obj, undefined); jest.spyOn(obj, null); jest.spyOn(obj, maybeMethod) where maybeMethod is undefined because a lookup failed. TypeScript usually catches this at compile time, but JS callers and loosely-typed code do not.
Common situations: Refactor that introduced an optional method name computed at runtime that became undefined; passing an options object as the second argument by mistake; copy-paste of a spy call with the key removed.
Related errors
- Cannot use spyOn on a primitive value; ${typeOfObject} given
- Property `${methodKey}` does not exist in the provided objec
- Cannot spy on the `${methodKey}` property because it is not
- Property `${propertyKey}` does not exist in the provided obj
- Property `${propertyKey}` is not declared configurable
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/5a142810594a690a.json.
Report an issue: GitHub.