diff --git a/CHANGELOG.md b/CHANGELOG.md index 163703719..2f3380774 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +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`). +- 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 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..86d6e1b31 --- /dev/null +++ b/src/matchers/__tests__/jest-globals-types.test.tsx @@ -0,0 +1,23 @@ +// 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 '../..'; + +// 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> { + toBeCustom(): R; + } +} + +// 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'); + expect(element).not.toBeDisabled(); +}); diff --git a/src/matchers/types.ts b/src/matchers/types.ts index c2b4ef0f5..22c9a91ca 100644 --- a/src/matchers/types.ts +++ b/src/matchers/types.ts @@ -287,9 +287,8 @@ 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`. 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 {} }