Property 'closeTo' does not exist on type 'Expect'

Viewed 540
test('compare float in object properties', () => {
  expect({
    title: '0.1 + 0.2',
    sum: 0.1 + 0.2,
  }).toEqual({
    title: '0.1 + 0.2',
    sum: expect.closeTo(0.3, 5),
  });
});

Property 'closeTo' does not exist on type 'Expect'.

Why am I getting this error ?

1 Answers

First, I'd ensure @types/jest has the same version as jest package does.

As a last resort, you can extend type definition as we do for custom matchers:

interface CustomMatchers<R = unknown> {
  closeTo(delta: number, value: number): R;
}

declare global {
  namespace jest {
    interface Expect extends CustomMatchers {}
  }
}
Related