jestjs/jest · error · Error

<spyOn> : No method name supplied Usage: spyOn(<object>, <me

Error message

<spyOn> : No method name supplied
Usage: spyOn(<object>, <methodName>)

What it means

Thrown by SpyRegistry.spyOn when the methodName argument is undefined (spyRegistry.ts:96). The second argument names the method to replace; omitting it (or passing undefined) leaves the registry unable to look up the property to spy on.

Source

Thrown at packages/jest-jasmine2/src/jasmine/spyRegistry.ts:96

    this.allowRespy = function (allow) {
      this.respy = allow;
    };

    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 {

View on GitHub (pinned to 8e6d128e4a)

Solutions

  1. Supply the literal method name: spyOn(obj, 'methodName').
  2. If the name is dynamic, validate it first: if (!name) throw new Error('method name required'); spyOn(obj, name);.
  3. Use TypeScript to type the second arg as keyof typeof obj so an undefined is rejected at compile time.
  4. Double-check the spy call was not truncated by an auto-formatter that removed a line.

Example fix

// before
spyOn(obj);
// after
spyOn(obj, 'methodName');
Defensive patterns

Strategy: validation

Validate before calling

if (methodName === undefined || methodName === null) {
  throw new Error('spyOn: methodName argument is required');
}
spyOn(obj, methodName);

Type guard

const hasMethodName = <T extends object>(o: T, k: PropertyKey): k is keyof T =>
  typeof k === 'string' && k in o;

Prevention

When it happens

Trigger: spyOn(obj) with only one argument, spyOn(obj, undefined), spyOn(obj, methodName) where methodName is computed from a variable that resolved to undefined, or destructuring that forgot to pull the name.

Common situations: Refactor renaming a method but forgetting the spy call; dynamically computing the method name from a config object whose key is missing; copy-paste from a template leaving the second slot empty; mocking with spread args that drop the name.

Related errors


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