3.1.1 • Published 4 years ago

jest-helpers v3.1.1

Weekly downloads
33
License
MIT
Repository
github
Last release
4 years ago

jest-helpers

npm version Build Status Coverage Status

Benefits

  • When you rename a class, the name of your test for it will automatically update
  • When you rename any of the following, your test will get a TypeScript error until you update your test:
    • A method on a class
    • A field on a class
    • An exported function inside a module
  • Has useful functions for creating mocks:
    • partialOf<T>(partial: Partial<T>): T
    • deepPartialOf<T>(partial: DeepPartial<T>): T

Install

npm install jest-helpers --save-dev

Usage

./greeter.ts

export class Greeter {
  getGreeting(name: string) {
    return `Hello ${name}`;
  }
}

export function showGreeting(greeter: Greeter, name: string) {
  const greeting = greeter.getGreeting(name)
  console.log(greeting)
}

./greeter.test.ts

import { describeClass, describeFunction, describeMethod, partialOf } from 'jest-helpers';
import greeterModule = require('greeter');
import { Greeter, showGreeting } from './greeter';

describeClass(Greeter, () => {
  describeMethod(Greeter, 'getGreeting', () => {
    it('should return a personalised greeting', () => {
      const greeter = new Greeter();
      expect(greeter.getGreeting('Joe')).toEqual('Hello Joe');
    });
  });
});

describeFunction(greeterModule, 'showGreeting', () => {
  it('should log greeting to the console', () => {
    const greeterMock = partialOf<Greeter>({
      getGreeting: jest.fn().mockReturnValue('yo!')
    });

    jest.spyOn(console, 'log');

    showGreeting(greeterMock, 'Joe');

    expect(greeterMock.getGreeting).toHaveBeenCalledWith('Joe');
    expect(console.log).toHaveBeenCalledWith('yo!');
  });
});

Running jest --verbose will output something like

  example/greeter.ts
    Greeter
      getGreeting
        ✓ should return a personalised greeting (4ms)
    showGreeting
      ✓ should log greeting to the console (2ms)

If you rename your Greeter class, it will automatically update the test description.

If you rename your Greeter.getGreeting method, you will get a TypeScript error in your test until you update your test to match the new name.

If you rename your showGreeting function, you will get a TypeScript error in your test until you update your test to match the new name.

Contributing

Got an issue or a feature request? Log it.

Pull-requests are also welcome. 😸

3.1.1

4 years ago

3.1.0

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.15

6 years ago

1.1.14

6 years ago

1.1.13

6 years ago

1.1.12

6 years ago

1.1.11

6 years ago

1.1.10

6 years ago

1.1.9

6 years ago

1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.0

6 years ago