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
- Inspect the descriptor (Object.getOwnPropertyDescriptor) to see which of get/set exists.
- Use the matching accessType ('get' for reads, 'set' for writes).
- 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
- Inspect the descriptor to learn which side (get/set) exists.
- Spy only on the accessType the property actually defines.
- Write a small helper that picks the right accessType automatically.
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
- Property `${String(propertyKey)}` does not exist in the prov
- Property `${String(propertyKey)}` is not declared configurab
- Cannot spy on the ${String(propertyKey)} property because it
- ${RECEIVED_COLOR('received')} value must be a mock or spy fu
- ${RECEIVED_COLOR('received')} value must be a mock function
AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10).
Data as JSON: /api/errors/12a71e908bdc295c.
Report an issue: GitHub.