From 94478cff43b07f9c2dd33bbf9943e14188175d99 Mon Sep 17 00:00:00 2001 From: shahjahan Date: Fri, 27 Nov 2026 14:29:46 +0500 Subject: [PATCH 1/4] fix: augment expect module so matcher types survive other augmentations Fixes #1968. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 +++ .../__tests__/jest-globals-types.test.tsx | 25 +++++++++++++++++++ src/matchers/types.ts | 11 ++++---- 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 src/matchers/__tests__/jest-globals-types.test.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index 163703719..42f97d2a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,10 @@ with v14. - Matcher type declarations no longer depend on the global `jest` namespace, fixing `TS2694` in TypeScript projects without `@types/jest` (e.g. when using `@jest/globals` or Vitest with `skipLibCheck: false`). +- Matcher types no longer disappear from the `@jest/globals` `expect` when another declaration + also augments the `expect` module (e.g. `expo-router/testing-library`), which made every + matcher call fail with `TS2339`. The types now augment `expect`, where Jest declares + `Matchers`, instead of `@jest/expect`, which only re-exports it. ## 14.1.0 diff --git a/src/matchers/__tests__/jest-globals-types.test.tsx b/src/matchers/__tests__/jest-globals-types.test.tsx new file mode 100644 index 000000000..895bb2b5f --- /dev/null +++ b/src/matchers/__tests__/jest-globals-types.test.tsx @@ -0,0 +1,25 @@ +// `@jest/globals` ships with `jest`. +// eslint-disable-next-line import/no-extraneous-dependencies +import { expect, test } from '@jest/globals'; +import * as React from 'react'; +import { Text } from 'react-native'; + +import { render, screen } from '../..'; + +// Another augmentation of `expect`, as libraries like `expo-router/testing-library` add. It must +// not drop the built-in matcher types from the `@jest/globals` `expect`. `yarn typecheck` checks it. +declare module 'expect' { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + interface Matchers, T = unknown> { + toBeTestOnlyMatcher(): R; + } +} + +test('built-in matchers are typed on the @jest/globals expect', async () => { + await render(Hello); + + const element = screen.getByText('Hello'); + expect(element).toBeOnTheScreen(); + expect(element).toHaveTextContent('Hello'); + expect(element).not.toBeDisabled(); +}); diff --git a/src/matchers/types.ts b/src/matchers/types.ts index c2b4ef0f5..ce41683d9 100644 --- a/src/matchers/types.ts +++ b/src/matchers/types.ts @@ -287,9 +287,10 @@ declare global { } } -// Explicit `@jest/globals` `expect` matchers. -// @ts-expect-error: Invalid module name in augmentation, module '@jest/expect' cannot be found -declare module '@jest/expect' { - // eslint-disable-next-line @typescript-eslint/no-empty-object-type - interface Matchers> extends JestNativeMatchers {} +// Explicit `@jest/globals` `expect` matchers. Augment `expect`, which declares `Matchers`, not +// `@jest/expect`, which only re-exports it: an augmentation through the re-export is dropped when +// another declaration also augments `expect`. +declare module 'expect' { + // eslint-disable-next-line @typescript-eslint/no-empty-object-type, @typescript-eslint/no-unused-vars + interface Matchers, T = unknown> extends JestNativeMatchers {} } From b2da034eec13473ab05bccae8a55f09403936e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Tue, 1 Dec 2026 11:06:39 +0100 Subject: [PATCH 2/4] tweaks --- src/matchers/__tests__/jest-globals-types.test.tsx | 4 +--- src/matchers/types.ts | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/matchers/__tests__/jest-globals-types.test.tsx b/src/matchers/__tests__/jest-globals-types.test.tsx index 895bb2b5f..0b6f5027f 100644 --- a/src/matchers/__tests__/jest-globals-types.test.tsx +++ b/src/matchers/__tests__/jest-globals-types.test.tsx @@ -1,4 +1,3 @@ -// `@jest/globals` ships with `jest`. // eslint-disable-next-line import/no-extraneous-dependencies import { expect, test } from '@jest/globals'; import * as React from 'react'; @@ -6,8 +5,7 @@ import { Text } from 'react-native'; import { render, screen } from '../..'; -// Another augmentation of `expect`, as libraries like `expo-router/testing-library` add. It must -// not drop the built-in matcher types from the `@jest/globals` `expect`. `yarn typecheck` checks it. +// Third-party `expect` augmentation (e.g. `expo-router`) must not drop our matcher types. declare module 'expect' { // eslint-disable-next-line @typescript-eslint/no-unused-vars interface Matchers, T = unknown> { diff --git a/src/matchers/types.ts b/src/matchers/types.ts index ce41683d9..22c9a91ca 100644 --- a/src/matchers/types.ts +++ b/src/matchers/types.ts @@ -287,9 +287,7 @@ declare global { } } -// Explicit `@jest/globals` `expect` matchers. Augment `expect`, which declares `Matchers`, not -// `@jest/expect`, which only re-exports it: an augmentation through the re-export is dropped when -// another declaration also augments `expect`. +// Explicit `@jest/globals` `expect`. Augment `expect`, not `@jest/expect`, which only re-exports `Matchers`. declare module 'expect' { // eslint-disable-next-line @typescript-eslint/no-empty-object-type, @typescript-eslint/no-unused-vars interface Matchers, T = unknown> extends JestNativeMatchers {} From dd4dca6042ba53672404222cc4a53632a5b87321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Tue, 1 Dec 2026 11:09:21 +0100 Subject: [PATCH 3/4] tighten test --- src/matchers/__tests__/jest-globals-types.test.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/matchers/__tests__/jest-globals-types.test.tsx b/src/matchers/__tests__/jest-globals-types.test.tsx index 0b6f5027f..86d6e1b31 100644 --- a/src/matchers/__tests__/jest-globals-types.test.tsx +++ b/src/matchers/__tests__/jest-globals-types.test.tsx @@ -5,17 +5,17 @@ import { Text } from 'react-native'; import { render, screen } from '../..'; -// Third-party `expect` augmentation (e.g. `expo-router`) must not drop our matcher types. +// Simulates a third-party `expect` augmentation (e.g. `expo-router`). declare module 'expect' { // eslint-disable-next-line @typescript-eslint/no-unused-vars interface Matchers, T = unknown> { - toBeTestOnlyMatcher(): R; + toBeCustom(): R; } } -test('built-in matchers are typed on the @jest/globals expect', async () => { +// Type-level test: fails `yarn typecheck` if the augmentation drops our matchers. +test('matchers are typed on @jest/globals expect', async () => { await render(Hello); - const element = screen.getByText('Hello'); expect(element).toBeOnTheScreen(); expect(element).toHaveTextContent('Hello'); From 2ddd8c85847632c0224e6c763ce4e7484a7b9940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Tue, 1 Dec 2026 11:10:05 +0100 Subject: [PATCH 4/4] changelog --- CHANGELOG.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42f97d2a7..2f3380774 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,10 +29,8 @@ with v14. - Matcher type declarations no longer depend on the global `jest` namespace, fixing `TS2694` in TypeScript projects without `@types/jest` (e.g. when using `@jest/globals` or Vitest with `skipLibCheck: false`). -- Matcher types no longer disappear from the `@jest/globals` `expect` when another declaration - also augments the `expect` module (e.g. `expo-router/testing-library`), which made every - matcher call fail with `TS2339`. The types now augment `expect`, where Jest declares - `Matchers`, instead of `@jest/expect`, which only re-exports it. +- Matchers on `expect` from `@jest/globals` no longer fail type-checking with `TS2339` when + another library also adds matchers to `expect` (e.g. `expo-router/testing-library`). ## 14.1.0