jestjs/jest · error · Error

Property does not have access type

Error message

Property ${propertyName} does not have access type ${accessType}

What it means

Thrown by SpyRegistry._spyOnProperty when the property descriptor has no member matching the requested accessType (spyRegistry.ts:183-189). For example spying with 'get' on a property that only has a setter, or with 'set' on a get-only accessor.

Solutions

  1. Inspect the descriptor and pick the matching accessType: Object.getOwnPropertyDescriptor(obj, 'p').
  2. For plain data fields use spyOn(obj, 'p') without accessType.
  3. For getter-only properties spy with 'get'; for setter-only spy with 'set'.
  4. Add a test-time setter/getter via defineProperty if you need the other direction.

Example fix

// before
spyOn(store, 'readOnlyConfig', 'set'); // only a getter exists
// after
spyOn(store, 'readOnlyConfig', 'get');
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: spyOn(obj, 'readOnly', 'set') on a getter-only property, spyOn(obj, 'writeOnly', 'get') on a setter-only property, or passing an invalid accessType like 'value' for a property with no get/set.

Common situations: Confusing the two accessor directions; copy-paste of a get-spy for a set-only property; using the property-spy form on a plain data field whose descriptor has writable but no get/set.

Related errors


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

Appendix: source

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

      let descriptor: PropertyDescriptor | undefined;
      try {
        descriptor = Object.getOwnPropertyDescriptor(obj, propertyName);
      } catch {
        // IE 8 doesn't support `definePropery` on non-DOM nodes
      }

      if (!descriptor) {
        throw new Error(getErrorMsg(`${propertyName} property does not exist`));
      }

      if (!descriptor.configurable) {
        throw new Error(
          getErrorMsg(`${propertyName} is not declared configurable`),
        );
      }

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

      if (obj[propertyName] && isSpy(obj[propertyName])) {
        if (this.respy) {
          return obj[propertyName];
        } else {
          throw new Error(
            getErrorMsg(`${propertyName} has already been spied upon`),
          );
        }
      }

      const originalDescriptor = descriptor;
      const spiedProperty = createSpy(propertyName, descriptor[accessType]);

View on GitHub (pinned to 8e6d128e4a)