添加babel-plugin-rewire

时间:2016-11-04 17:00:33

标签: reactjs webpack babeljs react-boilerplate

我正在使用https://github.com/mxstbr/react-boilerplate 并且想要模拟和监视我的单元测试的导入。 我在package.json

中导入了babel-plugin-rewire作为dev依赖项
 "babel-plugin-rewire": "^1.0.0",

我还添加了babel-plugin-rewire作为测试插件

"test": {
    "plugins": [
      "istanbul","babel-plugin-rewire"
    ]
  }

在package.json中的babel.test部分。

最后在我的webpack.test.babel.js中添加了

loader: 'babel?plugins=babel-plugin-rewire',

然后当我跑

npm run test

我得到了一个奇怪的错误

PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
  TypeError: undefined is not an object (evaluating 'cov_1uiq851gv7.b[1][0]')
  at webpack:///app/reducers.js:36:4 <- test-bundler.js:51890

我错过了什么?有人设置反应 - 样板与babel-plugin-rewire?

奇怪的部分是reducers.js,它在我的test.bundler.js中排除了错误。 添加reducers.js

import { combineReducers } from 'redux-immutable';
import { fromJS } from 'immutable';
import { LOCATION_CHANGE } from 'react-router-redux';
import languageProviderReducer from 'containers/LanguageProvider/reducer';
import driverReducer from 'containers/DriverList/reducer';
import vehicleReducer from 'containers/VehicleList/reducer';

// Initial routing state
const routeInitialState = fromJS({
  locationBeforeTransitions: null,
});


function routeReducer(state = routeInitialState, action) {
  switch (action.type) {
    /* istanbul ignore next */
    case LOCATION_CHANGE:
      return state.merge({
        locationBeforeTransitions: action.payload,
      });
    default:
      return state;
  }
}

export default function createReducer(asyncReducers) {
  return combineReducers({
    route: routeReducer,
    driverList: driverReducer,
    vehicleList: vehicleReducer,
    language: languageProviderReducer,
    ...asyncReducers,
  });
}

0 个答案:

没有答案