jestjs/jest · error · Error

No method name supplied

Error message

No method name supplied

What it means

Thrown by `SpyRegistry.spyOn` in spyRegistry.ts:96 when `methodName === void 0`. The spy registry needs an explicit method name string to know which property to replace on the host object.

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 f49721c78e)

Solutions

  1. Supply the method name as a string literal: `spyOn(obj, 'methodName')`.
  2. If the name is dynamic, assert it is a string before spying.
  3. Double-check the call arity — `spyOn` requires exactly two positional args (object, name).

Example fix

// before
spyOn(dateUtils)
// after
spyOn(dateUtils, 'formatDate')
Defensive patterns

Strategy: validation

Validate before calling

if (typeof methodName !== 'string') { throw new TypeError('spyOn needs a method name'); }
spyOn(obj, methodName);

Type guard

const isMethodName = (s: unknown): s is string => typeof s === 'string' && s.length > 0;

Prevention

When it happens

Trigger: Calling `spyOn(obj)` with only one argument, or `spyOn(obj, undefined)` / `spyOn(obj, someVar)` where `someVar` is undefined.

Common situations: Typo in a dynamic spy (`spyOn(obj, methodName)` with a typo'd variable); refactor that introduced an optional chain producing undefined; misremembering jasmine's `spyOn(obj, 'name')` arity.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/3cb0554ce362ebc4.json. Report an issue: GitHub.