我正在使用 webpack-provide-plugin 导入 React。
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);
});
但是在使用 jest 运行 react 组件测试时,我收到错误
ReferenceError: React is not defined
当然,因为react没有显式导入。除了显式导入和放弃 Provide-plugin 之外,还有其他方法解决这个问题吗?
最佳答案
您可以为 Jest 创建一个安装文件,如下所示:
//jest.setup.js
window.React = require('react');
并将其添加到 Jest 配置中:
"setupFiles": [ "<rootDir>/jest.setup.js" ]
,
http://facebook.github.io/jest/docs/configuration.html#setupfiles-array
关于reactjs - Jest 与 webpack 提供插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39487906/