jest-mock-getter-setter
v1.1.1
Published
A template for creating npm packages using TypeScript and VSCode
Downloads
1
Readme
jest-mock-getter-setter
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.
Install
npm install jest-mock-getter-setter
Usage
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);
});
});
});