After doing this hard work, everything is working fine now.
Side Effects:
In VS Code, Files explorer marks test files red.
Also, tests not showing icon of success or failure in coding area.
But no issues when running the test cases.
Solution to that is preety easy.
Either restart VS Code
Or restart Jest Runner and all the side-effects will be removed.
Error: Uncaught [Error: mockConstructor(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.]
Downgraded react-scripts to 3.4.0, installed jest-environment-jsdom-sixteen, running react-scripts test --env=jsdom-sixteen, just can't get it to run.
Which if course, was resetting the implementation. I think the issue is react-scripts 4 added "resetMocks": true, as a default so it might be fixed by just:
"jest": {
"resetMocks": false,
}
in your package.json
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
When using jest.mock with React + Typescript, first test case fails because render method gets an object instead of component.
Solution:
The component in the example is using named Export, but my component was using Default Export.
So, I tried this code:
But still I was getting an error, so I used this code:
After that I also had to downgrade
react-scripts
to3.4.0
and add a new package:And last change was to use the newly installed library in package.json. Please see new environment added to scripts:
After doing this hard work, everything is working fine now.
Side Effects:
In VS Code, Files explorer marks test files red.
Also, tests not showing icon of success or failure in coding area.
But no issues when running the test cases.
Solution to that is preety easy.
Either restart VS Code
Or restart Jest Runner and all the side-effects will be removed.
Credits:
github.com/facebook/jest/issues/97... - suggestions on changing packages
thoughtbot.com/blog/mocking-react-... -
__esModule: true
comes from this post.Cannot get this solution to work.
Downgraded
react-scripts
to 3.4.0, installedjest-environment-jsdom-sixteen
, runningreact-scripts test --env=jsdom-sixteen
, just can't get it to run.Finally! I had:
Which if course, was resetting the implementation. I think the issue is react-scripts 4 added
"resetMocks": true,
as a default so it might be fixed by just:in your package.json