-
Notifications
You must be signed in to change notification settings - Fork 399
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
Type definitions not properly loaded when importing from jest's setupTests #45
Comments
It would be helpful to see the I can think of a couple possible solutions:
You can try either one of the above - no need to do both. |
Thanks! Your tips got me in the right direction. I did not even have to add the file to the includes, but rather remove it from the excludes. Turns out Anyway, thanks again. I'm closing this. |
Great! It looks like excluding that file was deliberate: wmonk/create-react-app-typescript@8e24948 It should probably be handled in the
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs"
},
"exclude": [
"node_modules",
"build",
"scripts",
"webpack"
]
} |
Hmmm, but when I do that, vscode still flags me the error. And not just vscode, but CRA's
BTW I nevertheless opened the ticket wmonk/create-react-app-typescript#371 though your response does clarify a bit about the why. I'll try your second method and see how it goes. |
Maybe the |
Wouldn't know. I'm using create-react-app-typescript and this is my first TypeScript project ever. I'll continue digging and hopefully also someone in that ticket will respond. |
For anyone else wondering here: We were having this problem mainly with VSCode. Project ran tests fine without warnings. Creating a import "jest-dom/extend-expect"; "fixed" it for us :) |
The issue for us turned out to be that the setup file was still a |
I still have problems, even though my
Failing PR: https://github.com/MoeSauber/change/pull/50 |
|
My solve is to define a file called "scripts/setupEnv.d.ts", and include it in |
Maybe this would help: import '@testing-library/jest-dom/extend-expect'; Also add |
add a file named 'jest-dom-d.ts' in src/@types include |
@kirill-konshin THANK YOU! I added this at the top of my test file, and it fixed the issue. |
I have a My test compiles & passes, but VSCode still complains that But this is not a proper fix as the method Any ideas? Why doesn't this just work out-of-the-box like other "npm @types" packages? What am I missing? |
There are differences with regular packages. For starters a regular package you'll most likely import explicitly what you need from it in the modules you are using it. Custom jest matches OTOH are not imported in the modules you use it, but in a central location, and they also are not used explicitly from the dependency, but they are injected instead into the custom matchers namespace provided by whatever That being said, importing jest-dom from the file configured in jest's |
@gnapse ah ok. Thanks for the response & info. In the end my problem was I had a mismatched version of "@types/jest" (24.x vs latest of everything else) that was causing a conflict with the Matcher interface 🤦♂️ (it was not explicitly complaining about that though, so it was hard to find). Also my project is a components library so a little different project configs than CRA. Through this problem I also learnt more about the tsconfig "types" option, originally I had "types": ["node", "react", "jest"], remove all of them I learnt then loads everything in "rootDirs" i.e default @types. This plus fixing my versions meant everything was fine... Until I came across compile issues with styled-components v5 @types weird react-native dependency. So my final tsconfig is
with jest config (I removed the
Noting that jest-styled-components does a similar thing you mentioned regarding the injected custom matchers. Just for anyone else maybe working with these packages. Cheers |
thank you @xaun. adding
|
Only this worked for me. 😃 Thanks man. I love React and am grateful for the React team and their work but seriously why is CRA so messy 😆... it doesn't even run out of the box without errors... This error came out right after |
In my React Native app, this was the fix: import '@testing-library/jest-native/extend-expect'; |
This commit fixes testing-library/jest-dom#45.
This commit fixes testing-library/jest-dom#45.
It might be a silly oversight from my side, but if none of the above worked for you, try to first install the types from Definitely Typed: npm install --save-dev @types/testing-library__jest-dom This library does not come with types out of the box, and nowhere in the readme docs it reminds you of that. I've created a PR to fix this: #496 |
jest-dom
version: 1.10.0node
version: 8.11.3react-testing-library
version: 4.1.4Relevant code or config:
Problem description:
When importing
jest-dom/extend-expect
, as instructed in the README, within jest'ssetupTestFrameworkScriptFile
file, and using TypeScript at the same time, I get TypeScript errors in my test files saying that this library's custom matchers are not found:However, when I import
jest-dom/extend-expect
from within the very text files that need those matchers it all works. Moreover, it even works if I import it in just one of those files, which suddenly removes the TS warning from a second test file, without having to import it again from that second test file.Suggested solution:
This StackOverflow answer may be part of the solution, but I wanted to bring this up first to see if someone more knowledgeable with TypeScript can help. @jgoz maybe?
The text was updated successfully, but these errors were encountered: