1

I recently started working on an already mature project where I am integrating the Jest testing framework. The project uses React with Webpack. In the webpack config, the team is using webpack.ProvidePlugin to provide React, lodash, and classnames (not what I would have done, but whatever).

The problem I'm running into, is that most of the React classes at the top of the file contain something like React.Component, which works fine because of the webpack base config. However, when I run the corresponding test file, I just get React is not defined unless I explicitly require React.

There is a webpack.test.config file that requires the base webpack config, but nevertheless I can't seem to get it so that I can run the tests without explicitly including import React from 'react' at the top of every file. I don't want to have to go manually import React in every file, anyone have any suggestions of what to do?

3
  • stackoverflow.com/questions/30233357/… Commented Jun 28, 2017 at 22:55
  • I ended up just removing the global imports and importing React in every file after discussing with the team. Neither of these solutions worked for me. Commented Jun 30, 2017 at 22:02
  • Yeah that's been my strategy as well. I prefer not to do globals. Sorry I couldn't be of more help! Commented Jul 2, 2017 at 0:55

2 Answers 2

2

I think it is an anti-pattern to use globals in this case. I would just remove the globals and include react in every file

Sign up to request clarification or add additional context in comments.

Comments

0

You could try using jest's resolver and when you get to the react import return the path to react. OR you could use jest's setupFile and import React from 'react'; in there

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.