[英]How to use babel-plugin-rewire with React?
I'm seeing this error: 我看到此错误:
✖ should dispatch profileAction
PhantomJS 1.9.8 (Linux 0.0.0)
TypeError: 'undefined' is not a function (evaluating '_Header2.default.__Rewire__('profileAction', function (a) {
a();
})')
at /home/piousbox/Dropbox/projects/colombia_tailors_react/app/app.tests.js:74904 <- webpack:///app/components/App/__tests__/Header-test.jsx:23:53
My test looks like this: 我的测试看起来像这样:
it('should dispatch profileAction', (done) => {
Header.__Rewire__('profileAction', (a) => { a() })
let r = ReactTestUtils.renderIntoDocument(
<Provider store={store}>
<Header router={{location:{}}} />
</Provider>)
let elem = ReactDOM.findDOMNode(r)
})
My production code looks like this: 我的生产代码如下所示:
import { profileAction } from '../../actions/profileActions'
profileAction // eslint-disable-line no-unused-expressions
class Header extends React.Component {
constructor(props) {
super(props)
props.dispatch(profileAction())
}
render () { ... }
}
What am I doing wrong? 我究竟做错了什么? I am looking to assert that profileAction() was called when the component Header was mounted.
我想断言在安装组件Header时调用了profileAction()。
The issue was misconfiguration of webpack, I now have this and it works: 问题是webpack的配置错误,我现在有了这个,它可以工作:
{ test: /\\.jsx?$/, exclude: /node_modules/, - loader: 'babel-loader?plugins=babel-plugin-rewire', + loader: 'babel-loader', query: { - presets: [ 'babel-preset-es2015', 'babel-preset-stage-2', 'babel-preset-react' ] + presets: [ 'babel-preset-es2015', 'babel-preset-stage-2', 'babel-preset-react' ], + plugins: [ 'babel-plugin-rewire' ], } },
I added { query: { plugins: [ 'babel-plugin-rewire' ] } }
to webpack/loaders.js, and it works. 我向webpack / loaders.js添加了
{ query: { plugins: [ 'babel-plugin-rewire' ] } }
rewire { query: { plugins: [ 'babel-plugin-rewire' ] } }
,并且可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.