Jest tests failing without module mock
See original GitHub issueHi,
Jest tests fail on my project created with create-react-app.
One of the errors I get is:
TypeError: Cannot read property 'propTypes' of undefined
at withDirection (node_modules/react-with-direction/dist/withDirection.js:131:24)
This is very likely related to the provider (and the fact that tests don’t have them). Other tests fail, but don’t produce warnings realted to direction (although that is the cause).
For now I’ve mocked the react-with-direction module:
src/mocks/react-with-direction.js
let reactWithDirection = jest.genMockFromModule('react-with-direction');
reactWithDirection = {
...reactWithDirection,
default: component => component
};
module.exports = reactWithDirection;
This seems to resolve it.
Is there a recommended solution for this?
Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:7
Top Results From Across the Web
Bypassing module mocks - Jest
Jest allows you to mock out whole modules in your tests, which can be useful for testing if your code is calling functions...
Read more >Jest tests fail due to failed initialisation in mocked module
I've got const globalVar = initialiseWithEnvVar(). which fails to initialise with no .env file. I would've thought I wouldn't need an env ...
Read more >Mocking a JavaScript Class with Jest, two ways to make it easier
A guide on how to mock a JavaScript class using Jest, ... the type is maintained for the parameters the tests will work...
Read more >A guide to module mocking with Jest - Emma Goto
When writing Jest unit tests, I always struggle to remember the syntax for mocking modules. So this post is intended as a part-guide,...
Read more >Jest Testing like a Pro - Tips and tricks
If the methods you want to test are asynchronous, be it callbacks, promises or async, it is not a problem with Jest.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
A bit more explanation:
A component that is wrapped with
withDirection
will use direction context from the closest DirectionProvider. If DirectionProvider is not present anywhere in the tree, the withDirection HOC will use a LTR direction context by default. So technically DirectionProvider is not required.In your tests, if you are testing something unrelated to direction you can choose to not provide a DirectionProvider. For example:
In this example I’m using Enzyme’s
dive
method to dive through the withDirection HOC.Thanks @yzimet. That’s very useful!