question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jest tests failing without module mock

See original GitHub issue

Hi,

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:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
yzimetcommented, Apr 18, 2018

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:

const wrapper = shallow(
  <ForwardsLabel
    onPress={onPress}
  />
).dive();

expect(wrapper.prop('foo')).to.equal('bar');

In this example I’m using Enzyme’s dive method to dive through the withDirection HOC.

0reactions
dac09commented, Apr 19, 2018

Thanks @yzimet. That’s very useful!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found