After upgrading the version of Jest to the latest one my unit tests for pipe are failing. The initial error I was getting was:
jest Cannot set property of [object Object] which has only a getter
This error was not present in the earlier version (24).
I found a similar problem but unfortunately the applied solution did not help in my case.
This is my pipe:
@Pipe({
name: 'standPermissionIds',
})
export class StandPermissionIdsPipe implements PipeTransform {
transform(value: Stands): StandIdentifiers | undefined {
if (!value) {
return;
}
return extractStandIdentifiers(value);
}
}
and tests for that (before applying the solution to get rid off 'getter' error):
import { StandPermissionIdsPipe } from '@sam-hmi/shared/pipes/stand-permission-ids.pipe';
import { extractStandIdentifiers } from '@sam-hmi/utils';
describe('StandPermissionIdsPipe', () => {
let pipe;
beforeEach(() => {
pipe = new StandPermissionIdsPipe();
(extractStandIdentifiers as jest.Mock) = jest.fn();
});
it('should call extractStandIdentifiers and return the value', () => {
pipe.transform('test');
expect(extractStandIdentifiers).toBeCalledWith('test');
});
});
and after applying it:
import { StandPermissionIdsPipe } from '@sam-hmi/shared/pipes/stand-permission-ids.pipe';
describe('StandPermissionIdsPipe', () => {
let pipe;
let extractStandIdentifiers;
beforeEach(() => {
pipe = new StandPermissionIdsPipe();
extractStandIdentifiers = jest.mock('@sam-hmi/utils', () => ({
...jest.requireActual('@sam-hmi/utils'),
}));
});
it('should call extractStandIdentifiers and return the value', () => {
pipe.transform('test');
expect(extractStandIdentifiers).toHaveBeenCalledWith('test');
});
});
In that case the error message is about something else but probably because I screwed applying the solution:

And this is how the extractStandIdentifiers looks like:
export const extractStandIdentifiers = (stand: Stands): StandIdentifiers => {
const { standId, terminalFk, apronFk, stationFk } = stand;
return {
standId,
terminalId: terminalFk ? terminalFk.terminalId : undefined,
apronId: apronFk ? apronFk.apronId : undefined,
stationId: stationFk ? stationFk.stationId : undefined,
};
};
Can someone please explain to me how to solve this and what has changed and why I started getting these errors in the newer version of Jest?