Property 'toHaveTextContent' does not exist on type 'Matchers<void> & ...' (and any other matcher from library)
See original GitHub issuereact-nativeorexpo:expo@testing-library/react-nativeversion:11.5.0jest-preset:jest-exporeact-nativeversion:0.70.5nodeversion:v16.17.1@testing-library/jest-nativeversion:5.3.3jestversion: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)

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.

Matchers interface from Jest:

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.
Issue Analytics
- State:
- Created 9 months ago
- Comments:14

Top Related StackOverflow Question
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.
And, matchers interface extension for
@types/jestand@jest/globalssimultaneously:Code example here. You need to comment/delete interface extension in
/node_modules/@testing-library/jest-native/extend-expect.d.tsto test the@types/jestcase.