Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions src/matchers/__tests__/jest-globals-types.test.tsx
Original file line number Diff line number Diff line change
@@ -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<R extends void | Promise<void>, 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(<Text>Hello</Text>);
const element = screen.getByText('Hello');
expect(element).toBeOnTheScreen();
expect(element).toHaveTextContent('Hello');
expect(element).not.toBeDisabled();
});
9 changes: 4 additions & 5 deletions src/matchers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<R extends void | Promise<void>> extends JestNativeMatchers<R> {}
// 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<R extends void | Promise<void>, T = unknown> extends JestNativeMatchers<R> {}
}