jestjs/jest · error · Error

Property `${String(methodKey)}` does not exist in the provid

Error message

Property `${String(methodKey)}` does not exist in the provided object

What it means

Thrown by jest.spyOn when object[methodKey] is falsy. The spy reads the existing value to wrap it, so a missing/undefined property cannot be spied on. Note this is a truthiness check, so a property explicitly set to 0/''/false also triggers it.

Source

Thrown at packages/jest-mock/src/index.ts:1296

      (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') {
        throw new TypeError(
          `Cannot spy on the \`${String(
            methodKey,
          )}\` property because it is not a function; ${this._typeOf(
            original,
          )} given instead.${
            typeof original === 'object'
              ? ''
              : ` If you are trying to mock a property, use \`jest.replaceProperty(object, '${String(
                  methodKey,

View on GitHub (pinned to 8e6d128e4a)

Solutions

  1. Verify the property exists and is truthy on the exact object reference (console.log(obj[methodKey])).
  2. Spy on the prototype or the object that actually owns the method.
  3. If the value is legitimately falsy, reconsider whether spyOn is the right tool (it only wraps functions).

Example fix

// before
jest.spyOn(repo, 'find'); // find is defined on Repo.prototype, repo.find is inherited
// after
jest.spyOn(Repo.prototype, 'find');
Defensive patterns

Strategy: validation

Validate before calling

if (!obj[methodKey]) {
  throw new Error(`Cannot spyOn missing/falsy property ${String(methodKey)}`);
}
jest.spyOn(obj, methodKey);

Type guard

const hasTruthyProp = <T extends object>(o: T, k: keyof T): boolean =>
  Boolean(o[k]);

Prevention

When it happens

Trigger: jest.spyOn(obj,'nonExistent'); jest.spyOn(obj,'count') when obj.count === 0; spying on a method only present on a sibling object.

Common situations: Misspelled method name; the method lives on the prototype/parent and the wrong reference was used; tree-shaking stripped the method; the property is set to a falsy non-function value.

Related errors


AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10). Data as JSON: /api/errors/c8a44997a738e58f. Report an issue: GitHub.