React Jest导入不可变对象未定义

时间:2017-07-24 15:58:10

标签: reactjs jestjs immutable.js

我是Jest和React的新手,正在为React网络项目设置Jest单元测试,当我尝试在Reducer上进行此操作时遇到此问题。 似乎当被测试的文件包含Record时,它在npm测试时变为未定义(当npm开始时它运行正常)。 我在test.js文件中放了jest.unmock(' immutable'),jest.autoMockOff(),但似乎没有用......挖掘解决方案,但我什么都没发现......

这是文件/输出/设置

./ reducer.js

export const initialState = new Record({ ... })()
...
export default function reducer(state=initialState, action) {...}

./__测试__ / reducer.test.js

import reducer,{initialState} from '../reducer'
describe('reducer',()=>{

    it('initial state',()=>{
        expect(reducer(undefined,{})).toEqual(
            initialState
        )
    })
})

npm测试输出

 FAIL  src\reducers\__test__\reducer.test.js

 ● Test suite failed to run

ReferenceError: initialState is not defined

  at reducer (src/reducers/reducer.js:88:303)
  at node_modules/redux/lib/combineReducers.js:53:24
  at Array.forEach (native)
  at assertReducerShape (node_modules/redux/lib/combineReducers.js:51:25)
  at combineReducers (node_modules/redux/lib/combineReducers.js:107:5)
  at Object.<anonymous> (src/reducers/index.js:34:48)
  at Object.<anonymous> (src/store/configureStore.development.js:19:255)
  at Object.<anonymous> (src/store/configureStore.js:4:20)
  at Object.<anonymous> (src/index.js:10:23)
  at Object.<anonymous> (src/utils.js:11:161)
  at Object.<anonymous> (src/actions/actions.js:373:324)
  at Object.<anonymous> (src/reducers/reducer.js:64:229)
  at Object.<anonymous> (src/reducers/__test__/reducer.test.js:7:21)
  at process._tickCallback (internal/process/next_tick.js:109:7)

的package.json

 "devDependencies": {
"babel-jest": "^20.0.3",
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"enzyme": "^2.9.1",
"enzyme-to-json": "^1.5.1",
"jest": "^20.0.4",
"jest-immutable-matchers": "^1.5.0",
"react-addons-test-utils": "^15.6.0",
"react-dom": "^15.6.1",
"react-scripts": "^1.0.10",
"react-test-renderer": "^15.6.1",
"redux-devtools": "^3.3.1",
"redux-devtools-dock-monitor": "^1.1.1",
"redux-devtools-log-monitor": "^1.1.1",
"redux-mock-store": "^1.2.3"
},

.babelrc

{
"presets":["react","es2015","stage-0","stage-2"]
}

2 个答案:

答案 0 :(得分:1)

我有同样的问题,并且能够用两种分辨率解决它

  1. 包括正确的babel-presets(删除react-app);
  2. <强>的package.json

      "babel": {
        "presets": [
          "env",
          "react",
          "stage-0"
        ],
        "env": {
          "test": {
            "presets": [
              "env",
              "react",
              "stage-0"
            ]
          }
        }
    

    <强>依赖关系 npm install --save-dev babel-preset-env babel-preset-react babel-preset-stage0

    1. 确保我的本地目录结构没有相同的名称;例如,我的结构如下:
    2. ~/sites/project @htdocs -> src src/ (create-react-app <root>) build/ config/ src/ components/ ... index.js package.json

      请注意,我将'src'作为根名称,并将'src'作为包含react文件的文件夹。出于某种原因,Immutable Records抱怨这一点,所以我只是将'src'目录中的一个改为不同的(即'source')。

答案 1 :(得分:0)

我为商店及其工作文件创建了export default { // STORE stories:{}, } 文件。我希望这对你有所帮助。

import initialState from '../initialState'

并导入如下

@EnableSwagger2