Skip to content

Types need Jest's global jest namespace: TS2694 in to-have-style.d.ts without @types/jest #1962

Open

Description

@YevheniiKotyrlo

Describe the bug

dist/matchers/to-have-style.d.ts types the matcher's this as jest.MatcherContext, a member of the global jest namespace that only @types/jest declares. The package's own declare global { namespace jest … } in dist/matchers/types.d.ts declares Matchers and not MatcherContext, and @types/jest is neither a dependency nor a peer dependency. So a TypeScript project that imports @testing-library/react-native without @types/jest (a Jest project that imports its globals from @jest/globals, or a Vitest one) fails its type check under skipLibCheck: false:

node_modules/@testing-library/react-native/dist/matchers/to-have-style.d.ts(4,48): error TS2694: Namespace 'global.jest' has no exported member 'MatcherContext'.

Expected behavior

Importing @testing-library/react-native type-checks in a project without @types/jest, and toHaveStyle keeps its types where Jest's are loaded.

Steps to Reproduce

  1. In an empty folder, install the package and its peers, without @types/jest:
    npm install @testing-library/react-native@14.1.0 react@19.1.0 react-native@0.79.7 test-renderer@1.3.1 typescript@5 @types/react@19
  2. Add tsconfig.json:
    {
      "compilerOptions": {
        "strict": true,
        "skipLibCheck": false,
        "noEmit": true,
        "target": "ES2022",
        "lib": ["ES2022"],
        "module": "preserve",
        "moduleResolution": "bundler",
        "jsx": "react-jsx",
        "types": []
      },
      "files": ["render.tsx"]
    }
  3. Add render.tsx:
    import { render, screen } from '@testing-library/react-native';
    import { Text } from 'react-native';
    
    export const renderHello = async (): Promise<void> => {
      await render(<Text>Hello</Text>);
      screen.getByText('Hello');
    };
  4. Run npx tsc -p .

Expected: no output. Actual: the TS2694 error above.

Reproducer: https://gist.github.com/YevheniiKotyrlo/49628750d2dc1a8fe98c14d74983e926. node repro.mjs builds main (564a842), packs it, runs the steps above, and exits 1 while the error shows:

@testing-library/react-native main at 564a842 (2026-11-05)
== tsc --noEmit
node_modules/@testing-library/react-native/dist/matchers/to-have-style.d.ts(4,48): error TS2694: Namespace 'global.jest' has no exported member 'MatcherContext'.
RESULT: the package's types need Jest's global jest namespace (TS2694)

React Native 0.79.7 keeps the output to this one error: 0.88.1's own declarations fail the same check (react/react-native#59647).

Screenshots

None.

Versions

npmPackages:
    @testing-library/react-native: file:../testing-library-react-native-14.1.0.tgz => 14.1.0 
    react: ^19.1.0 => 19.1.0 
    react-native: ^0.79.7 => 0.79.7 
    test-renderer: ^1.3.1 => 1.3.1
typescript Version 5.9.3

Activity

  1. YevheniiKotyrlo commented on Nov 12, 2026

    @YevheniiKotyrlo
    Author

    The patch I apply to 14.1.0 types toHaveStyle's this with the two members it reads, isNot and equals, so the declaration names no jest namespace:

    diff --git a/dist/matchers/to-have-style.d.ts b/dist/matchers/to-have-style.d.ts
    index 9c73054ed8099ddd23c13ecab848c1c908e68277..b14062d60c402f83febe14d791335babce7c2142 100644
    --- a/dist/matchers/to-have-style.d.ts
    +++ b/dist/matchers/to-have-style.d.ts
    @@ -1,7 +1,10 @@
     import type { ImageStyle, StyleProp, TextStyle, ViewStyle } from 'react-native';
     import type { TestInstance } from 'test-renderer';
     export type Style = ViewStyle | TextStyle | ImageStyle;
    -export declare function toHaveStyle(this: jest.MatcherContext, instance: TestInstance, style: StyleProp<Style>): {
    +export declare function toHaveStyle(this: {
    +    readonly isNot: boolean;
    +    readonly equals: (a: unknown, b: unknown) => boolean;
    +}, instance: TestInstance, style: StyleProp<Style>): {
         pass: boolean;
         message: () => string;
     };
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