marchaos / jest-mock-extended

Type safe mocking extensions for Jest https://www.npmjs.com/package/jest-mock-extended
MIT License
836 stars 57 forks source link

add functionality to safe mock functions #17

Closed regevbr closed 4 years ago

regevbr commented 4 years ago

Example of the feature:

type MyFunction = (x: string) => Promise<number>;

const mocked = mockFn<MyFunction>();
mocked.mockResolvedValue(5);
marchaos commented 4 years ago

Hey. I'm in 2 minds about this. I feel that jest already provides this functionality already. I haven't checked, but assume jest.fn does not have a generic Param for doing this?

regevbr commented 4 years ago

Hi @marchaos, your calledWithFn also have generic params, same as jest.fn but I want your library capabilities, and I want auto inference of types and not passing them manually...

The following example can't be achieved by jest

 type MyFn = (x: number, y: number) => Promise<string>;
 const fn = mockFn<MyFn>();
 fn.calledWith(1, 2).mockReturnValue('str');
marchaos commented 4 years ago

Yep. Makes sense. I'll merge this in for now, but am planning on adding "mock" to the best object as "mockType" so might need to do something similar with this at that point. I will keep it backwards compatible!