Utility to create a mock for getter & setter. Useful if you want to use dependency injection without having to deal with the actual object's constructor.
npm install jest-mock-getter-setter
describe('setMockProperty', () => {
describe('setMockProperty', () => {
it('should mock a property of MockedObject', () => {
// mock some module with jest.createMockFromModule
const mock = jest.createMockFromModule<ClientRequest>('http');
setMockProperty(mock, 'finished', true);
expect(mock.finished).toBe(true);
});
it('should should return a setter that can be asserted', () => {
const mock = jest.createMockFromModule<ClientRequest>('http');
const [, setter] = setMockProperty(mock, 'finished', true);
mock.finished = false;
expect(setter).toBeCalledTimes(1);
expect(setter).toBeCalledWith(false);
});
it('should should return a getter that can be asserted', () => {
const mock = jest.createMockFromModule<ClientRequest>('http');
const [getter] = setMockProperty(mock, 'finished', true);
mock.finished;
expect(getter).toBeCalledTimes(1);
});
});
});