Repository: facebook/jest
File: packages/jest-types/__typetests__/expect.test.ts (TypeScript)

Code before:
// mock matchers

expectType<void>(expect(jest.fn()).toBeCalled());
expectError(expect(jest.fn()).toBeCalled('value'));
expectType<void>(expect(jest.fn()).toHaveBeenCalled());
expectError(expect(jest.fn()).toHaveBeenCalled(false));

expectType<void>(expect(jest.fn()).toBeCalledTimes(1));
expectError(expect(jest.fn()).toBeCalledTimes('twice'));
expectError(expect(jest.fn()).toBeCalledTimes());
expectType<void>(expect(jest.fn()).toHaveBeenCalledTimes(3));
expectError(expect(jest.fn()).toHaveBeenCalledTimes(true));
expectError(expect(jest.fn()).toHaveBeenCalledTimes());

expectType<void>(expect(jest.fn()).toBeCalledWith());
expectType<void>(expect(jest.fn()).toBeCalledWith('value'));
expectType<void>(expect(jest.fn()).toBeCalledWith('value', 123));
expectType<void>(expect(jest.fn()).toHaveBeenCalledWith());
expectType<void>(expect(jest.fn()).toHaveBeenCalledWith(123));
expectType<void>(expect(jest.fn()).toHaveBeenCalledWith(123, 'value'));
expectType<void>(expect(jest.fn()).toHaveBeenCalledWith(123, 'value'));

// type inference for "CalledWith" matchers parameters
const jestFnWithParams = jest.fn((a: string, b: number) => {});
expectType<void>(expect(jestFnWithParams).toHaveBeenCalledWith('value', 123));
expectError(expect(jestFnWithParams).toHaveBeenCalledWith(123, 'value'));

expectType<void>(expect(jest.fn()).lastCalledWith());
expectType<void>(expect(jest.fn()).lastCalledWith('value'));
expectType<void>(expect(jest.fn()).lastCalledWith('value', 123));
expectType<void>(expect(jest.fn()).toHaveBeenLastCalledWith());
expectType<void>(expect(jest.fn()).toHaveBeenLastCalledWith(123));
expectType<void>(expect(jest.fn()).toHaveBeenLastCalledWith(123, 'value'));

expectType<void>(expect(jest.fn()).nthCalledWith(2));
expectType<void>(expect(jest.fn()).nthCalledWith(1, 'value'));
expectType<void>(expect(jest.fn()).nthCalledWith(1, 'value', 123));
expectError(expect(jest.fn()).nthCalledWith());
expectType<void>(expect(jest.fn()).toHaveBeenNthCalledWith(2));
expectType<void>(expect(jest.fn()).toHaveBeenNthCalledWith(1, 'value'));
expectType<void>(expect(jest.fn()).toHaveBeenNthCalledWith(1, 'value', 123));
expectError(expect(jest.fn()).toHaveBeenNthCalledWith());

expectType<void>(expect(jest.fn()).toReturn());
expectError(expect(jest.fn()).toReturn('value'));
expectType<void>(expect(jest.fn()).toHaveReturned());
expectError(expect(jest.fn()).toHaveReturned(false));

expectType<void>(expect(jest.fn()).toReturnTimes(1));
expectError(expect(jest.fn()).toReturnTimes('twice'));
expectError(expect(jest.fn()).toReturnTimes());

Review comment (suggestion):
Let’s test optional params as well?

```suggestion
expectType<void>(
  expect(
    jest.fn<(a: string, b: number, c?: boolean) => void>(),
  ).toHaveBeenCalledWith('value', 123),
);
expectType<void>(
  expect(
    jest.fn<(a: string, b: number, c?: boolean) => void>(),
  ).toHaveBeenCalledWith('value', 123, true),
);
expectError(
  expect(
    jest.fn<(a: string, b: number, c?: boolean) => void>(),
  ).toHaveBeenCalledWith(123, 'value'),
);
expectError(
  expect(
    jest.fn<(a: string, b: number, c?: boolean) => void>(),
  ).toHaveBeenCalledWith('value', 123, 'nope'),
);
```

Code after:
// mock matchers

expectType<void>(expect(jest.fn()).toBeCalled());
expectError(expect(jest.fn()).toBeCalled('value'));
expectType<void>(expect(jest.fn()).toHaveBeenCalled());
expectError(expect(jest.fn()).toHaveBeenCalled(false));

expectType<void>(expect(jest.fn()).toBeCalledTimes(1));
expectError(expect(jest.fn()).toBeCalledTimes('twice'));
expectError(expect(jest.fn()).toBeCalledTimes());
expectType<void>(expect(jest.fn()).toHaveBeenCalledTimes(3));
expectError(expect(jest.fn()).toHaveBeenCalledTimes(true));
expectError(expect(jest.fn()).toHaveBeenCalledTimes());

expectType<void>(expect(jest.fn()).toBeCalledWith());
expectType<void>(expect(jest.fn()).toBeCalledWith('value'));
expectType<void>(expect(jest.fn()).toBeCalledWith('value', 123));
expectType<void>(expect(jest.fn()).toHaveBeenCalledWith());
expectType<void>(expect(jest.fn()).toHaveBeenCalledWith(123));
expectType<void>(expect(jest.fn()).toHaveBeenCalledWith(123, 'value'));

/**
 * type inference for "CalledWith" matchers parameters
 */
expectError(expect(jest.fn<(a: string) => void>()).toHaveBeenCalledWith(123));
expectError(
  expect(jest.fn<(a: string) => void>()).toHaveBeenNthCalledWith(1, 123),
);
expectError(
  expect(jest.fn<(a: string) => void>()).toHaveBeenLastCalledWith(123),
);
expectType<void>(
  expect(
    jest.fn<(a: string, b: number, c?: boolean) => void>(),
  ).toHaveBeenCalledWith('value', 123),
);
expectType<void>(
  expect(
    jest.fn<(a: string, b: number, c?: boolean) => void>(),
  ).toHaveBeenCalledWith('value', 123, true),
);
expectError(
  expect(
    jest.fn<(a: string, b: number, c?: boolean) => void>(),
  ).toHaveBeenCalledWith(123, 'value'),
);
expectError(
  expect(
    jest.fn<(a: string, b: number, c?: boolean) => void>(),
  ).toHaveBeenCalledWith('value', 123, 'not a boolean'),
);