From 94a6752c67bc061eb4b8fc192b3a28c38d33eb65 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sun, 6 Feb 2022 03:36:43 +0200 Subject: [PATCH] fix(expect): add type definitions for asymmetric `closeTo` matcher (#12304) --- CHANGELOG.md | 2 ++ packages/expect/src/types.ts | 1 + packages/jest-types/__typetests__/expect.test.ts | 11 +++++++++++ 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad40bf59222f..38e17f9242ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[expect]` Add type definitions for asymmetric `closeTo` matcher ([#12304](https://github.com/facebook/jest/pull/12304)) + ### Chore & Maintenance ### Performance diff --git a/packages/expect/src/types.ts b/packages/expect/src/types.ts index 53286977a83a..6b1d1b8560b0 100644 --- a/packages/expect/src/types.ts +++ b/packages/expect/src/types.ts @@ -74,6 +74,7 @@ interface AsymmetricMatchers { any(sample: unknown): AsymmetricMatcher; anything(): AsymmetricMatcher; arrayContaining(sample: Array): AsymmetricMatcher; + closeTo(sample: number, precision?: number): AsymmetricMatcher; objectContaining(sample: Record): AsymmetricMatcher; stringContaining(sample: string): AsymmetricMatcher; stringMatching(sample: string | RegExp): AsymmetricMatcher; diff --git a/packages/jest-types/__typetests__/expect.test.ts b/packages/jest-types/__typetests__/expect.test.ts index 3f6eae703433..37a6f9efb136 100644 --- a/packages/jest-types/__typetests__/expect.test.ts +++ b/packages/jest-types/__typetests__/expect.test.ts @@ -25,6 +25,17 @@ expectType(expect(['B']).toEqual(expect.not.arrayContaining(['A']))); expectError(expect(['A']).toEqual(expect.not.arrayContaining('A'))); expectError(expect(['A']).toEqual(expect.not.arrayContaining())); +expectType(expect(0.1 + 0.2).toEqual(expect.closeTo(0.3))); +expectType(expect(0.1 + 0.2).toEqual(expect.closeTo(0.3, 5))); +expectError(expect(0.1 + 0.2).toEqual(expect.closeTo('three'))); +expectError(expect(0.1 + 0.2).toEqual(expect.closeTo(0.3, false))); +expectError(expect(0.1 + 0.2).toEqual(expect.closeTo())); +expectType(expect(0.1 + 0.2).toEqual(expect.not.closeTo(0.3))); +expectType(expect(0.1 + 0.2).toEqual(expect.not.closeTo(0.3, 5))); +expectError(expect(0.1 + 0.2).toEqual(expect.not.closeTo('three'))); +expectError(expect(0.1 + 0.2).toEqual(expect.not.closeTo(0.3, false))); +expectError(expect(0.1 + 0.2).toEqual(expect.not.closeTo())); + expectType(expect({a: 1}).toEqual(expect.objectContaining({a: 1}))); expectError(expect({a: 1}).toEqual(expect.objectContaining(1))); expectError(expect({a: 1}).toEqual(expect.objectContaining()));