Skip to content

Property 'toHaveTextContent' does not exist on type 'Matchers<void> & ...' (and any other matcher from library) #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alexamy opened this issue Dec 15, 2022 · 15 comments · Fixed by #137

Comments

@alexamy
Copy link
Contributor

alexamy commented Dec 15, 2022

  • react-native or expo: expo
  • @testing-library/react-native version: 11.5.0
  • jest-preset: jest-expo
  • react-native version: 0.70.5
  • node version: v16.17.1
  • @testing-library/jest-native version: 5.3.3
  • jest version: 29.3.0

Relevant code or config:

expect(await screen.findByRole('alert')).toHaveTextContent(
  'Incorrect username or password'
);

What you did:

Just using any matcher from jest-native.

What happened:

Typescript complains for missed types. For example, for toHaveTextContent matcher:

Property 'toHaveTextContent' does not exist on type 'Matchers<void> & SnapshotMatchers<void, ReactTestInstance> &
Inverse<JestMatchers<void, ReactTestInstance>> & PromiseMatchers<ReactTestInstance>'.ts(2339)

image

Reproduction:

https://github.com/alexamy/react-native-testing-library-missing-matchers
or the same code in zip archive:
react-native-testing-library-missing-matchers-45392ed44fe4fa3de64723609f6a0f74227188a4.zip

Problem description:

There is no way to use custom matchers in typescript tests for obvious reason. This problem can be 'avoided' by skipping matcher typecheck with @ts-ignore or optionally skipping test typecheck altogether, but it is not really a solution.

Suggested solution:

Modify types in @testing-library/jest-native/extend-expect.d.ts to proper extend the matchers interface.

Btw, there is also import problem on line 3.

image

Matchers interface from Jest:
image

I've tried to make declare global in project with extended jest' Matchers interface, but it seems that something is missed, I didn't get any working result from my tries, this interface seems to be unchanged.

Can you help us fix this issue by submitting a pull request?

I can make a Pull Request with solution for this problem if someone can point me how to properly modify definition file.

@worldlee78
Copy link

Yeah, I am also experiencing this issue. Would be willing to help solve it.

@roni-castro
Copy link

roni-castro commented Dec 16, 2022

@alexamy Did you try installing @types/jest instead of using "@jest/globals"?

It seems there are some conflicts with the @jest/globals

Screen.Recording.2022-12-16.at.00.33.07.mov

@worldlee78
Copy link

worldlee78 commented Dec 16, 2022 via email

@roni-castro
Copy link

roni-castro commented Dec 16, 2022

I installed the @types/jest at the same time as I used @jest/globals but no dice. Are you suggesting that @types/jest would work where the recommended globals would not? I did have this working prior to switching but it feels like a step backwards to go from explicitly calling jest methods to going back to globals. I should mention that even when I try getting the matchers directly (instead of importing them all at once during setup) it doesn’t see the typescript there either, so you might be right (though i really don’t want to have to undo all the work of explicitly using the globals).

On Thu, Dec 15, 2022 at 7:34 PM Roni Castro @.> wrote: @alexamy https://github.com/alexamy Did you try installing @types/jest instead of using @./globals"? https://user-images.githubusercontent.com/24610813/208015970-3059ca21-444e-4109-8b96-bcf044af0bed.mov — Reply to this email directly, view it on GitHub <#136 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHTRV3CPWTICBEAYTJROIDWNPPLLANCNFSM6AAAAAATAHBGOU . You are receiving this because you commented.Message ID: @.***>

@worldlee78 It seems the declarations of this file are ignored when you import using @jest/globals, maybe because the global comes from jest.Expect and the @jest/globals from JestExpect
Screen Shot 2022-12-16 at 01 14 10
Screen Shot 2022-12-16 at 01 14 27

Screen Shot 2022-12-16 at 01 08 40

Screen.Recording.2022-12-16.at.01.23.38.mov

@alexamy
Copy link
Contributor Author

alexamy commented Dec 16, 2022

@roni-castro Thanks, I will try to use @types/jest to find the solution.

But I think that using implicit globals is a bad pratice because, according to Jest docs:

Getting started with Typescript:

@types/jest is a third party library maintained at DefinitelyTyped, 
hence the latest Jest features or versions may not be covered yet.

API:

The TypeScript examples from this page will only work as documented if you explicitly import Jest APIs

@alexamy
Copy link
Contributor Author

alexamy commented Dec 16, 2022

UPD: After reloading editor, the error goes away.

@roni-castro With @types/jest: 29.2.4 I got slightly different error:

Property 'toHaveTextContent' does not exist on type 'JestMatchers<ReactTestInstance>'.

image

expect is coming from jest.Expect:

image

I've made a new branch with @types/jest:
https://github.com/alexamy/react-native-testing-library-missing-matchers/blob/add-global-jest-types/__tests__/errors.test.tsx

UPD: After reloading editor, the error goes away.

@mdjastrzebski
Copy link
Collaborator

Maybe we could have a look at some other 3rd party Jest marchers package written in TS and see how they are solving the issue.

@alexamy
Copy link
Contributor Author

alexamy commented Dec 16, 2022

It looks like jest-dom has the same problem:

// Type definitions for @testing-library/jest-dom 5.14
// Project: https://github.com/testing-library/jest-dom
// Definitions by: Ernesto García <https://github.com/gnapse>
//                 John Gozde <https://github.com/jgoz>
//                 Seth Macpherson <https://github.com/smacpherson64>
//                 Andrew Leedham <https://github.com/AndrewLeedham>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Minimum TypeScript Version: 4.3

/// <reference types="jest" />

import { TestingLibraryMatchers } from './matchers';

declare global {
    namespace jest {
        interface Matchers<R = void, T = {}> extends TestingLibraryMatchers<typeof expect.stringContaining, R> {}
    }
}

image

@alexamy
Copy link
Contributor Author

alexamy commented Dec 16, 2022

So, the solution to the missing type definitions for matchers is to use @types/jest and dont use jest explicit globals import from @jest/globals at all.

Similar issue in jest-dom: testing-library/jest-dom#426

Anyone feel free to investigate further, but for now I close this issue.

@alexamy alexamy closed this as completed Dec 16, 2022
@alexamy
Copy link
Contributor Author

alexamy commented Dec 16, 2022

Following this comment (thanks @yordis), this may lead to the solution:

declare module '@jest/expect' {
  interface Matchers<R extends void | Promise<void>> {
    toBeDisabled(): R;
    toBeEmptyElement(): R;
    toBeEnabled(): R;
    toBeVisible(): R;
    toContainElement(element: ReactTestInstance | null): R;
    toHaveTextContent(text: string | RegExp, options?: { normalizeWhitespace: boolean }): R;
    toHaveProp(attr: string, value?: unknown): R;
    toHaveStyle(style: StyleProp<ViewStyle | TextStyle | ImageStyle>): R;
    toHaveAccessibilityState(state: AccessibilityState): R;
    toHaveAccessibilityValue(state: AccessibilityValueMatcher): R;

    /** @deprecated This function has been renamed to `toBeEmptyElement`. */
    toBeEmpty(): R;
  }
}

But it seems that this solution redefines already existing matchers from 'jest':
image

Made a new branch in repro repo with additional definition file:
https://github.com/alexamy/react-native-testing-library-missing-matchers/blob/fix-jest-native-typings/__tests__/globals.d.ts

@alexamy alexamy reopened this Dec 16, 2022
@mdjastrzebski
Copy link
Collaborator

@alexamy shoulden't TS theoretically merge the interfaces? https://www.typescriptlang.org/docs/handbook/declaration-merging.html#merging-namespaces

@alexamy
Copy link
Contributor Author

alexamy commented Dec 16, 2022

@mdjastrzebski Yes, but I think the reason for overwritting here is declare module. Thanks for the link to ts docs, it'll help to clear the issue.

@alexamy
Copy link
Contributor Author

alexamy commented Dec 16, 2022

@mdjastrzebski Thanks for a docs! Module augmentaion is the answer.
I think that this is the solution:

import { Matchers } from '@jest/expect';

declare module '@jest/expect' {
  interface Matchers<R extends void | Promise<void>> {
    toBeDisabled(): R;
    toBeEmptyElement(): R;
    toBeEnabled(): R;
    toBeVisible(): R;
    toContainElement(element: ReactTestInstance | null): R;
    toHaveTextContent(text: string | RegExp, options?: { normalizeWhitespace: boolean }): R;
    toHaveProp(attr: string, value?: unknown): R;
    toHaveStyle(style: StyleProp<ViewStyle | TextStyle | ImageStyle>): R;
    toHaveAccessibilityState(state: AccessibilityState): R;
    toHaveAccessibilityValue(state: AccessibilityValueMatcher): R;

    /** @deprecated This function has been renamed to `toBeEmptyElement`. */
    toBeEmpty(): R;
  }
}

@alexamy
Copy link
Contributor Author

alexamy commented Dec 16, 2022

And, matchers interface extension for @types/jest and @jest/globals simultaneously:

import { Matchers } from '@jest/expect';

interface JestNativeMatchers<R extends void | Promise<void>> {
  toBeDisabled(): R;
  toBeEmptyElement(): R;
  toBeEnabled(): R;
  toBeVisible(): R;
  toContainElement(element: ReactTestInstance | null): R;
  toHaveTextContent(text: string | RegExp, options?: { normalizeWhitespace: boolean }): R;
  toHaveProp(attr: string, value?: unknown): R;
  toHaveStyle(style: StyleProp<ViewStyle | TextStyle | ImageStyle>): R;
  toHaveAccessibilityState(state: AccessibilityState): R;
  toHaveAccessibilityValue(state: AccessibilityValueMatcher): R;

  /** @deprecated This function has been renamed to `toBeEmptyElement`. */
  toBeEmpty(): R;
}

// implicit jest globals, types coming from `@types/jest`
declare global {
  namespace jest {
    interface Matchers<R, T> extends JestNativeMatchers<R> {}
  }
}

// explicit jest globals, types coming from `@jest/globals`
declare module '@jest/expect' {
  interface Matchers<R extends void | Promise<void>> extends JestNativeMatchers<R> {}
}

Code example here.
You need to comment/delete interface extension in /node_modules/@testing-library/jest-native/extend-expect.d.ts to test the @types/jest case.

@mathewvaughan
Copy link

And, matchers interface extension for @types/jest and @jest/globals simultaneously:

import { Matchers } from '@jest/expect';

interface JestNativeMatchers<R extends void | Promise<void>> {
  toBeDisabled(): R;
  toBeEmptyElement(): R;
  toBeEnabled(): R;
  toBeVisible(): R;
  toContainElement(element: ReactTestInstance | null): R;
  toHaveTextContent(text: string | RegExp, options?: { normalizeWhitespace: boolean }): R;
  toHaveProp(attr: string, value?: unknown): R;
  toHaveStyle(style: StyleProp<ViewStyle | TextStyle | ImageStyle>): R;
  toHaveAccessibilityState(state: AccessibilityState): R;
  toHaveAccessibilityValue(state: AccessibilityValueMatcher): R;

  /** @deprecated This function has been renamed to `toBeEmptyElement`. */
  toBeEmpty(): R;
}

// implicit jest globals, types coming from `@types/jest`
declare global {
  namespace jest {
    interface Matchers<R, T> extends JestNativeMatchers<R> {}
  }
}

// explicit jest globals, types coming from `@jest/globals`
declare module '@jest/expect' {
  interface Matchers<R extends void | Promise<void>> extends JestNativeMatchers<R> {}
}

Code example here. You need to comment/delete interface extension in /node_modules/@testing-library/jest-native/extend-expect.d.ts to test the @types/jest case.

Shamelessly copied this into my jest.config.ts - one thing I had to add was a clarification on the R:

declare global {
    namespace jest {
        interface Matchers<R extends Promise<void>, T> extends JestNativeMatchers<R> { }
    }
}

This stopped my ts from complaining, although I admit I don't understand why this works.

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 a pull request may close this issue.

5 participants