Skip to content

Commit

Permalink
docs(readme): update example and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
codeandcats committed Oct 5, 2018
1 parent 32bbb94 commit f9dbae5
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 6 deletions.
75 changes: 74 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,78 @@
# jest-helpers
TypeScript helper functions for Jest which make your tests resilient to refactoring.
TypeScript helper functions for Jest to help make your tests resilient to refactoring.

## 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, describeModule, partialOf
} from 'jest-helpers';
import { Greeter, showGreeting } from './greeter';

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

describeFunction(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)
```

## Benefits
- ✅ If you rename your `greeter.test.ts` module, it will automatically update the test description.

- ✅ If you rename your `Greeter` class, it will automatically update the test description

- ✅ If you rename your `Greeter.getGreeting` method, it will automatically update the test description

- ✅ If you rename your `showGreeting` method, it will automatically update the test description

## Contributing
Got an issue or a feature request? [Log it](https://github.com/codeandcats/jest-helpers/issues).
Expand Down
22 changes: 20 additions & 2 deletions example/greeter.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describeClass, describeMethod, describeModule } from '../src/index';
import { Greeter } from './greeter';
import { describeClass, describeFunction, describeMethod, describeModule, partialOf } from '../src';
import { Greeter, showGreeting } from './greeter';

describeModule(() => {
describeClass(Greeter, () => {
Expand All @@ -10,4 +10,22 @@ describeModule(() => {
});
});
});

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

jest.spyOn(console, 'log').mockImplementation(() => {
//
});

showGreeting(greeterMock, 'Joe');

expect(greeterMock.getGreeting).toHaveBeenCalledWith('Joe');
// tslint:disable-next-line:no-console
expect(console.log).toHaveBeenCalledWith('yo!');
});
});
});
6 changes: 6 additions & 0 deletions example/greeter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ export class Greeter {
return `Hello ${name}`;
}
}

export function showGreeting(greeter: Greeter, name: string) {
const greeting = greeter.getGreeting(name);
// tslint:disable-next-line:no-console
console.log(greeting);
}
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface NamedFunction {
(...args: any[]): any;
}

export function innerDescribeClass(
function innerDescribeClass(
describe: DescribeFunction,
classToDescribe: Class<{}>,
describer: () => void
Expand Down Expand Up @@ -69,7 +69,7 @@ export function xdescribeClass(classToDescribe: Class<{}>, describer: () => void
innerDescribeClass(xdescribe, classToDescribe, describer);
}

export function innerDescribeFunction(
function innerDescribeFunction(
describe: DescribeFunction,
func: NamedFunction,
describer: () => void
Expand Down Expand Up @@ -113,7 +113,7 @@ type FunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends Function ? K : never
}[keyof T];

export function innerDescribeMethod<TInstance>(
function innerDescribeMethod<TInstance>(
describe: DescribeFunction,
methodName: FunctionPropertyNames<TInstance>,
describer: () => void
Expand Down

0 comments on commit f9dbae5

Please sign in to comment.