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

Code before:
expectError(expect(123).toHaveNthReturnedWith(5));
expectError(expect(jest.fn()).toHaveNthReturnedWith());

// snapshot matchers

expectType<void>(expect({a: 1}).toMatchSnapshot());
expectType<void>(expect({a: 1}).toMatchSnapshot('hint'));
expectError(expect({a: 1}).toMatchSnapshot(true));

expectType<void>(
  expect({
    date: new Date(),
    name: 'John Doe',
  }).toMatchSnapshot({
    date: expect.any(Date),
    name: expect.any(String),
  }),
);

expectType<void>(
  expect({
    date: new Date(),
    name: 'John Doe',
  }).toMatchSnapshot(
    {
      date: expect.any(Date),
      name: expect.any(String),
    },
    'hint',
  ),
);

expectError(
  expect({
    date: new Date(),
    name: 'John Doe',
  }).toMatchSnapshot({
    date: expect.any(Date),
    time: expect.any(Date),
  }),
);

expectType<void>(expect('abc').toMatchInlineSnapshot());
expectType<void>(expect('abc').toMatchInlineSnapshot('inline snapshot here'));
expectError(expect('abc').toMatchInlineSnapshot(true));

expectType<void>(
  expect({
    date: new Date(),
    name: 'John Doe',
  }).toMatchInlineSnapshot({

Review comment (suggestion):
It works with `() => unknown` just like before. Infers return value, if there is one. Errors if `received` is not a function.

Code after:
expectType<void>(expect(jest.fn()).toHaveReturnedWith(123));
expectType<void>(expect(jest.fn<() => number>()).toHaveReturnedWith(123));
expectError(expect(jest.fn<() => string>()).toHaveReturnedWith(123));
expectError(expect(123).toHaveReturnedWith(123));
expectError(expect(jest.fn()).toHaveReturnedWith());

expectType<void>(expect(jest.fn()).lastReturnedWith('value'));
expectType<void>(expect(jest.fn<() => string>()).lastReturnedWith('value'));
expectError(expect(jest.fn<() => number>()).lastReturnedWith('value'));
expectError(expect(123).lastReturnedWith('value'));
expectError(expect(jest.fn()).lastReturnedWith());

expectType<void>(expect(jest.fn()).toHaveLastReturnedWith(123));
expectType<void>(expect(jest.fn<() => number>()).toHaveLastReturnedWith(123));
expectError(expect(jest.fn<() => string>()).toHaveLastReturnedWith(123));
expectError(expect(123).toHaveLastReturnedWith(123));
expectError(expect(jest.fn()).toHaveLastReturnedWith());

expectType<void>(expect(jest.fn()).nthReturnedWith(1, 'value'));
expectType<void>(expect(jest.fn<() => string>()).nthReturnedWith(2, 'value'));
expectError(expect(jest.fn<() => number>()).nthReturnedWith(3, 'value'));
expectError(expect(123).nthReturnedWith(4, 'value'));
expectError(expect(123).nthReturnedWith(5));
expectError(expect(jest.fn()).nthReturnedWith());

expectType<void>(expect(jest.fn()).toHaveNthReturnedWith(1, 'value'));
expectType<void>(
  expect(jest.fn<() => string>()).toHaveNthReturnedWith(2, 'value'),
);
expectError(expect(jest.fn<() => number>()).toHaveNthReturnedWith(3, 'value'));
expectError(expect(123).toHaveNthReturnedWith(4, 'value'));
expectError(expect(123).toHaveNthReturnedWith(5));
expectError(expect(jest.fn()).toHaveNthReturnedWith());

// snapshot matchers

expectType<void>(expect({a: 1}).toMatchSnapshot());
expectType<void>(expect({a: 1}).toMatchSnapshot('hint'));
expectError(expect({a: 1}).toMatchSnapshot(true));

expectType<void>(
  expect({
    date: new Date(),
    name: 'John Doe',
  }).toMatchSnapshot({
    date: expect.any(Date),
    name: expect.any(String),
  }),
);

expectType<void>(