jest-mock-getter-setter
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

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

/jest-mock-getter-setter/

    Package Sidebar

    Install

    npm i jest-mock-getter-setter

    Weekly Downloads

    1

    Version

    1.1.1

    License

    MIT

    Unpacked Size

    7.43 kB

    Total Files

    5

    Last publish

    Collaborators

    • nandappputra