Skip to content

Commit

Permalink
Merge pull request #32864 from jerico-dev/fix-jest-fn
Browse files Browse the repository at this point in the history
[jest] fix jest.fn to match spec
  • Loading branch information
minestarks committed Feb 14, 2019
2 parents ae4b81d + a76bd22 commit fe74af5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions types/jest-in-case/jest-in-case-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function subtract(minuend: number, subtrahend: number) {
beforeEach(() => {
jest.spyOn(global, 'describe').mockImplementation((title, fn) => (fn as () => void)());
jest.spyOn(global, 'test').mockImplementation((name, fn) => (fn as () => void)());
global.test.skip = jest.fn((name, fn) => fn());
global.test.only = jest.fn((name, fn) => fn());
global.test.skip = jest.fn((_: string, fn: jest.EmptyFunction) => fn());
global.test.only = jest.fn((_: string, fn: jest.EmptyFunction) => fn());
});

afterEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion types/jest/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ declare namespace jest {
/**
* Creates a mock function. Optionally takes a mock implementation.
*/
function fn<T, Y extends any[]>(implementation: (...args: Y) => T): Mock<T, Y>;
function fn<T, Y extends any[]>(implementation?: (...args: Y) => T): Mock<T, Y>;
/**
* Use the automatic mocking system to generate a mocked version of the given module.
*/
Expand Down
9 changes: 7 additions & 2 deletions types/jest/jest-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,15 @@ const mock7 = jest.fn((arg: number) => arg);
const mock8: jest.Mock = jest.fn((arg: number) => arg);
// $ExpectType Mock<Promise<boolean>, [number, string, {}, [], boolean]>
const mock9 = jest.fn((a: number, _b: string, _c: {}, _iReallyDontCare: [], _makeItStop: boolean) => Promise.resolve(_makeItStop));
// $ExpectType Mock<never, [never]>
const mock10 = jest.fn((arg: never) => { throw new Error(arg); });
// $ExpectType Mock<never, []>
const mock10 = jest.fn(() => { throw new Error(); });
// $ExpectType Mock<unknown, [unknown]>
const mock11 = jest.fn((arg: unknown) => arg);
interface TestApi {
test(x: number): string;
}
// $ExpectType Mock<string, [number]>
const mock12 = jest.fn<ReturnType<TestApi["test"]>, ArgsType<TestApi["test"]>>();

// $ExpectType number
mock1('test');
Expand Down

0 comments on commit fe74af5

Please sign in to comment.