https://github.com/codeandcats/jest-helpers
TypeScript helper functions for Jest to help make your tests resilient to refactoring.
https://github.com/codeandcats/jest-helpers
Last synced: 3 months ago
JSON representation
TypeScript helper functions for Jest to help make your tests resilient to refactoring.
- Host: GitHub
- URL: https://github.com/codeandcats/jest-helpers
- Owner: codeandcats
- License: mit
- Created: 2018-10-05T00:10:35.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T21:37:07.000Z (over 3 years ago)
- Last Synced: 2026-02-01T07:50:35.304Z (5 months ago)
- Language: TypeScript
- Homepage:
- Size: 1.12 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 24
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# jest-helpers
Tests should be living documentation for your code, but often test descriptions get out of sync with your code. This library helps keep them in sync using TypeScript.
[](https://badge.fury.io/js/jest-helpers)
[](https://travis-ci.org/codeandcats/jest-helpers)
[](https://coveralls.io/github/codeandcats/jest-helpers?branch=master)
## 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(partial: Partial): T`
- `deepPartialOf(partial: DeepPartial): T`
## Install
```sh
npm install jest-helpers --save-dev
```
## Usage
`./greeter.ts`
```typescript
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`
```typescript
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({
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](https://github.com/codeandcats/jest-helpers/issues).
[Pull-requests](https://github.com/codeandcats/jest-helpers/pulls) are also welcome. 😸