jestjs/jest · error · Error

Property `${String(propertyKey)}` does not have access type

Error message

Property `${String(propertyKey)}` does not have access type ${accessType}

What it means

Thrown by _spyOnProperty when the descriptor exists but lacks the requested accessType — e.g., you asked for 'get' on a write-only setter, or 'set' on a read-only getter. descriptor[accessType] is undefined.

Source

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

      proto = Object.getPrototypeOf(proto);
    }

    if (!descriptor) {
      throw new Error(
        `Property \`${String(
          propertyKey,
        )}\` does not exist in the provided object`,
      );
    }

    if (!descriptor.configurable) {
      throw new Error(
        `Property \`${String(propertyKey)}\` is not declared configurable`,
      );
    }

    if (!descriptor[accessType]) {
      throw new Error(
        `Property \`${String(
          propertyKey,
        )}\` does not have access type ${accessType}`,
      );
    }

    const original = descriptor[accessType];

    if (!this.isMockFunction(original)) {
      if (typeof original !== 'function') {
        throw new TypeError(
          `Cannot spy on the ${String(
            propertyKey,
          )} property because it is not a function; ${this._typeOf(
            original,
          )} given instead.${
            typeof original === 'object'
              ? ''

View on GitHub (pinned to 8e6d128e4a)

Solutions

  1. Inspect the descriptor (Object.getOwnPropertyDescriptor) to see which of get/set exists.
  2. Use the matching accessType ('get' for reads, 'set' for writes).
  3. If only one side exists, spy on that side only.

Example fix

// before
jest.spyOn(store, 'data', 'set'); // store.data has only a getter
// after
const d = Object.getOwnPropertyDescriptor(store, 'data');
jest.spyOn(store, 'data', d?.get ? 'get' : 'set');
Defensive patterns

Strategy: validation

Validate before calling

const d = Object.getOwnPropertyDescriptor(obj, propertyKey);
if (!d || !d[accessType]) {
  throw new Error(`${String(propertyKey)} has no '${accessType}' accessor`);
}
jest.spyOn(obj, propertyKey, accessType);

Type guard

const hasAccessType = (o: object, k: PropertyKey, a: 'get' | 'set'): boolean => {
  const d = Object.getOwnPropertyDescriptor(o, k);
  return !!d && typeof d[a] === 'function';
};

Prevention

When it happens

Trigger: jest.spyOn(obj,'x','get') when x has only a setter; jest.spyOn(obj,'x','set') when x is a read-only getter.

Common situations: Guessing the wrong access type; the property's accessor shape differs from what the test assumes after an API change.

Related errors


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