Skip to content

Commit

Permalink
Merge pull request #19 from regevbr/#17
Browse files Browse the repository at this point in the history
add functionality to safe mock functions #17
  • Loading branch information
marchaos committed Jun 11, 2020
2 parents d45ff95 + 00eefdb commit b52325a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -89,11 +89,12 @@ provider.getSongs.calledWith(any()).mockReturnValue(['Saw her standing there']);
provider.getSongs.calledWith(anyString()).mockReturnValue(['Saw her standing there']);

```
You can also use ```calledWith()``` on its own to create a ```jest.fn()``` with the calledWith extension:
You can also use ```mockFn()``` to create a ```jest.fn()``` with the calledWith extension:

```ts
const fn = calledWithFn();
fn.calledWith(1, 2).mockReturnValue(3);
type MyFn = (x: number, y: number) => Promise<string>;
const fn = mockFn<MyFn>();
fn.calledWith(1, 2).mockReturnValue('str');
```

## Clearing / Resetting Mocks
Expand Down
25 changes: 21 additions & 4 deletions src/Mock.spec.ts
@@ -1,4 +1,4 @@
import mock, { mockClear, mockDeep, mockReset } from './Mock';
import mock, { mockClear, mockDeep, mockReset, mockFn } from './Mock';
import { anyNumber } from './Matchers';
import calledWithFn from './CalledWithFn';

Expand Down Expand Up @@ -117,9 +117,9 @@ describe('jest-mock-extended', () => {

describe('calledWith', () => {
test('can use calledWith without mock', () => {
const mockFn = calledWithFn();
mockFn.calledWith(anyNumber(), anyNumber()).mockReturnValue(3);
expect(mockFn(1, 2)).toBe(3);
const mockFunc = calledWithFn();
mockFunc.calledWith(anyNumber(), anyNumber()).mockReturnValue(3);
expect(mockFunc(1, 2)).toBe(3);
});

test('Can specify matchers', () => {
Expand Down Expand Up @@ -361,4 +361,21 @@ describe('jest-mock-extended', () => {
expect(mockObj.deepProp.getNumber(1)).toBe(4);
});
});

describe('function mock', () => {
test('should mock function', async () => {
type MyFn = (x: number, y: number) => Promise<string>;
const mockFunc = mockFn<MyFn>();
mockFunc.mockResolvedValue(`str`);
const result: string = await mockFunc(1, 2);
expect(result).toBe(`str`);
});
test('should mock function and use calledWith', async () => {
type MyFn = (x: number, y: number) => Promise<string>;
const mockFunc = mockFn<MyFn>();
mockFunc.calledWith(1, 2).mockResolvedValue(`str`);
const result: string = await mockFunc(1, 2);
expect(result).toBe(`str`);
});
})
});
9 changes: 9 additions & 0 deletions src/Mock.ts
Expand Up @@ -121,4 +121,13 @@ const mock = <T>(mockImplementation: DeepPartial<T> = {} as DeepPartial<T>, opts
return overrideMockImp(mockImplementation, opts);
};

export const mockFn = <
T extends Function,
A extends any[] = T extends (...args: infer AReal) => any ? AReal : any[],
R = T extends (...args: any) => infer RReal ? RReal : any
>(): CalledWithMock<R, A> & T => {
// @ts-ignore
return calledWithFn();
};

export default mock;

0 comments on commit b52325a

Please sign in to comment.