Jest with webpack provide plugin

I am using webpack-provision-plugin to import.

new webpack.ProvidePlugin({ "React": "react", }), 

//text.jsx

 let text = (props) => ( <div> <p class="text">this.props.text</p> </div> ) export default text 

//text.test.js

 import React from 'react'; import { shallow } from 'enzyme'; import text from 'text'; it('Renders text', () => { const wrapper = shallow(<text/>); expect(wrapper.hasClass("text")).toEqual(true); }); 

But during the test tests of the components with a joke, I get an error

 ReferenceError: React is not defined 

Of course, because the reaction is not imported explicitly. Is there a way to this problem besides explicit import and refusal to provide the plugin?

+6
source share
2 answers

You can create an installation file for Jest as follows:

 //jest.setup.js window.React = require('react'); 

and add it to the Jest configuration: "setupFiles": [ "<rootDir>/jest.setup.js" ] http://facebook.imtqy.com/jest/docs/configuration.html#setupfiles-array

+8
source

remove this line while you are proving this variable using webpack providing the plugin;

import React from "react";

0
source

All Articles