Skip to content

Matcher types disappear from expect when another declaration augments expect (Jest 30) #1968

Backlog

Description

@hicaro

Describe the bug

With Jest 30, the built-in matcher types (toBeOnTheScreen, toHaveTextContent, ...) vanish from expect imported from @jest/globals as soon as any other declaration augments the expect module. Every matcher call then fails type-checking with TS2339, although the matchers still work at runtime.

The library declares its matchers by augmenting @jest/expect:

declare module '@jest/expect' {
  interface Matchers<R extends void | Promise<void>> extends JestNativeMatchers<R> {}
}

@jest/expect 30 does not declare Matchers itself. It re-exports it from expect (export {Matchers}). An augmentation that reaches the interface through that re-export is dropped once anything augments expect directly. One real-world trigger is expo-router/testing-library, which augments expect with its router matchers. Another library augmenting expect would do the same, and so does a one-line local declaration (below).

Matching type parameters does not help. Declaring the augmentation on expect itself does.

Expected behavior

The matcher types stay on expect regardless of other augmentations of the expect module.

Steps to Reproduce

A test project using @jest/globals and @testing-library/react-native with Jest 30. The test below type-checks.

// screen.test.tsx
import { expect, test } from '@jest/globals';
import { render, screen } from '@testing-library/react-native';
import { Text } from 'react-native';

test('matcher types', async () => {
  await render(<Text>Hello</Text>);
  expect(screen.getByText('Hello')).toBeOnTheScreen();
});

Now add any augmentation of expect to the program, for example:

// another-augmentation.d.ts
export {};
declare module 'expect' {
  interface Matchers<R extends void | Promise<void>, T = unknown> {
    toBeAnything(): R;
  }
}

tsc --noEmit now reports, for every matcher call:

error TS2339: Property 'toBeOnTheScreen' does not exist on type 'Matchers<void, TestInstance> & SnapshotMatchers<void, TestInstance> & Inverse<JestMatchers<void, TestInstance>> & PromiseMatchers<TestInstance>'.

Removing another-augmentation.d.ts makes the errors go away.

Workaround (and a likely fix): declare the matchers on expect, where Jest 30 declares Matchers:

import type { JestNativeMatchers } from '@testing-library/react-native/dist/matchers/types';

declare module 'expect' {
  interface Matchers<R extends void | Promise<void>, T = unknown> extends JestNativeMatchers<R> {}
}

Versions

  • @testing-library/react-native: 14.0.1 (reproduced). The @jest/expect augmentation is unchanged in 14.1.0.
  • jest, @jest/globals, expect: 30.5.2
  • typescript: 6.0.3
  • react-native: 0.88.1, react: 19.3.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions