1.0.8 • Published 3 years ago

jest-auto-stub v1.0.8

Weekly downloads
762
License
MIT
Repository
github
Last release
3 years ago

jest-auto-stub

Automatically create type-safe fully stubbed instances of classes or interfaces.

Installation

npm install jest-auto-stub

Usage

import { reveal, stub } from 'jest-auto-stub';

interface MyInterface {
  foo(): string;
}

class MyClass {
  bar(): string {
    return 'foo';
  }
}

describe('stub', () => {
  describe('interface', () => {
    it('will return an object', () => {
      const s = stub<MyInterface>();
      expect(s).toBeDefined();
      expect(s).toMatchObject({});
    });

    it('will track calls to stubbed methods', () => {
      const s = stub<MyInterface>();
      s.foo();
      expect(s.foo).toHaveBeenCalled();
    });

    it('will be able to provide mock results', () => {
      const s = stub<MyInterface>();

      reveal(s).foo.mockReturnValueOnce('bar');

      const result = s.foo();

      expect(s.foo).toHaveBeenCalled();
      expect(result).toBe('bar');
    });
  });

  describe('class', () => {
    it('will return an object', () => {
      const s = stub<MyClass>();
      expect(s).toBeDefined();
      expect(s).toMatchObject({});
    });

    it('will track calls to stubbed methods', () => {
      const s = stub<MyClass>();
      s.bar();
      expect(s.bar).toHaveBeenCalled();
    });

    it('will be able to provide mock results', () => {
      const s = stub<MyClass>();

      {
        const mock = reveal(s);
        mock.bar.mockReturnValueOnce('hello');
        mock.bar.mockReturnValueOnce('world');
      }

      const result = [s.bar(), s.bar()];

      expect(s.bar).toHaveBeenCalled();
      expect(result[0]).toBe('hello');
      expect(result[1]).toBe('world');
    });
  });
});
1.0.8

3 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago