1.1.1 • Published 12 months ago

jest-mock-getter-setter v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

jest-mock-getter-setter

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

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);
    });
  });
});
1.1.1

12 months ago

1.1.0

12 months ago

1.0.0

12 months ago