Skip to content

fix: augment expect module so matcher types survive other augmentations - #1969

Open
shahjahanpak wants to merge 1 commit into
callstack:mainfrom
shahjahanpak:fix/1968-jest30-expect-augmentation
Open

shahjahanpak wants to merge 1 commit into
callstack:mainfrom
shahjahanpak:fix/1968-jest30-expect-augmentation

Conversation

@shahjahanpak

Copy link
Copy Markdown

Summary

Fixes #1968.

The matcher types augmented @jest/expect. In Jest 29 and Jest 30, @jest/expect does not declare
Matchers; it re-exports it from expect. When TypeScript reads another augmentation of expect
after the library's types (for example expo-router/testing-library, which augments expect in
build/testing-library/expect.d.ts), the augmentation through the re-export is dropped. Every
matcher call on the @jest/globals expect then fails with TS2339, although the matchers work
at runtime. With skipLibCheck: false, the old block also reports TS2428, because it declares
Matchers with one type parameter and expect declares two.

This PR augments expect directly, with the same type parameters as Jest 29 and 30
(<R extends void | Promise<void>, T = unknown>), and removes the @jest/expect block:

  • With the expect block in place, a build that keeps the @jest/expect block gives the same
    result in every case of the matrix below, so the old block adds nothing.
  • Its @ts-expect-error fails as unused (TS2578) as soon as the program imports
    @jest/globals, which the new regression test does.

The global jest namespace block is unchanged. The global expect from @types/jest was not
affected by this defect.

Test plan

  1. New test src/matchers/__tests__/jest-globals-types.test.tsx: it augments expect with a
    test-only matcher and calls the built-in matchers on the @jest/globals expect.
    yarn typecheck on the old types.ts fails:
    src/matchers/__tests__/jest-globals-types.test.tsx(20,19): error TS2339: Property 'toBeOnTheScreen' does not exist on type 'Matchers<void, TestInstance> & SnapshotMatchers<void, TestInstance> & ...
    src/matchers/__tests__/jest-globals-types.test.tsx(21,19): error TS2339: Property 'toHaveTextContent' does not exist on type ...
    src/matchers/__tests__/jest-globals-types.test.tsx(22,23): error TS2339: Property 'toBeDisabled' does not exist on type 'JestMatchers<void, TestInstance>'.
    src/matchers/types.ts(291,1): error TS2578: Unused '@ts-expect-error' directive.
    src/matchers/types.ts(294,13): error TS2428: All declarations of 'Matchers' must have identical type parameters.
    
    With this PR: 0 errors.
  2. Packed package (yarn build, npm pack) installed in isolated projects with TypeScript 6.0.3,
    React Native 0.88.0-rc.1, and either Jest 30 (expect@30.5.2) or Jest 29 (expect@29.7.0).
    The test file is the one from the issue. Matrix: skipLibCheck true or false; @jest/globals
    only, or also the global expect with @types/jest; and these extra expect augmentations:
    none, a local file that loads first, a local file imported after the library, a package
    imported after the library (two type parameters), and a package imported after the library
    with one type parameter (the expo-router form).
    • Before: every case with an augmentation that loads after the library fails with TS2339 on
      each matcher call, on Jest 29 and Jest 30. With skipLibCheck: false it also reports TS2428.
      A local file that loads first does not trigger the defect.
    • After: no error from this library in any case. With skipLibCheck: false, the error count
      from other packages' types is the same before and after.
  3. A project without Jest installed (expect cannot be resolved), with skipLibCheck true and
    false: no error from this library, before or after.
  4. yarn validate (typecheck, 956 tests, lint, format) and yarn typecheck:react-19_2 pass.

Not tested: pnpm and Yarn PnP layouts, TypeScript versions other than 6.0.3, and a real
expo-router install (simulated by a package with the same declaration).

🤖 Generated with Claude Code

Fixes callstack#1968.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant