jest-native: Property 'toBeOnTheScreen' does not exist on type 'JestMatchers' (TS project)
react-nativeorexpo: RN@testing-library/react-nativeversion: ^11.4.0jest-preset: “react-native”react-nativeversion: 0.70.6nodeversion: 16
Relevant code or config:
const menuTitle = screen.getByText("My wonderful title");
expect(menuTitle).toBeOnTheScreen(); // tsc error
What you did:
Try to use custom matcher in a TS project that check types with tsc.
What happened:
Typescript check error.
Reproduction:
const menuTitle = screen.getByText("My wonderful title");
expect(menuTitle).toBeOnTheScreen(); // tsc error
Problem description:
Custom matcher of jest-native are not exposed as TS types. So a TS project have issue when build with TS compiler (tsc):
Ex:
Property 'toBeOnTheScreen' does not exist on type 'JestMatchers<ReactTestInstance>' (TS project)
Suggested solution:
expose types in package.json
About this issue
- Original URL
- State: open
- Created a year ago
- Comments: 20
@mdjastrzebski
If affects all matchers extended by jest-native, none of them show up in the intellisense / are underlined taht the property doesn’t exist. Same for toBeEnabled, toBeDisabled etc.
Although, this seemed to help in my
tsconfig.json@Aure77 I had the same problem, in my setup I had
.jest/setup.tswhich meant that theincludeoption intsconfig.jsonwasn’t including this file (since it wassrc/**/*). Maybe you have a similar issue.I had same issue. All my config files were ok.
I fixed it importing:
import '@testing-library/jest-native/';on every test.Example:
@mdjastrzebski Yes, that change in
tsconfig.jsonwas the missing part, thank you. So to summarise I have:jest-setup.tsfile with:tsconfig.jsonI used a triple slash directive to get the correct types added:
This was in a
.d.tsfile that the ts compiler had visibility of (e.g.src/types/jest.d.ts)I experience the same. It only goes away if like @emflomed17 mentions, you import it on the test or extend expect inline in the test file.
The only other thing to mention is I’m in a yarn workspace within a monorepo? Not sure that matters though. The error clearly goes away when you bring in the import line to the test file specifically. The tests all pass whether or not TS has an error about the matcher
package.json config
jest-setup.js config
Talking about VS Code intellisense, I’ve tried every workaround and only following solutions worked
tsconfig.jsonimport '@testing-library/jest-native/extend-expect';to every.testfilenot sure why the
declarations.d.tssolution is not working - is there anything additional I have to do? If I declare some random module there and try to import it, then the VS Code intellisense finds that module, so the file is loaded properly.Using latest
"@testing-library/jest-native": "^5.4.2",with"typescript": "^5.0.4"@emflomed17 did you follow the installation instructions at https://github.com/testing-library/jest-native#usage ?
In correct setting adding
import '@testing-library/jest-native/extend-expect';to yourjest-setup.js(make sure it’ssetupFilesAfterEnv) file should be enough to make matchers work correctly.Hey, you have forgot to install
testing-library/jest-nativeInstall it and then check it will work.Thanks,