ReactJS - 开玩笑测试错误'意外令牌......'

时间:2018-02-08 14:27:08

标签: reactjs testing jest

我正在尝试在切换到笔记本电脑后运行开玩笑测试。以前,在计算机上每个测试都启动并且没有返回语法/运行时错误,但现在当我尝试在笔记本电脑上运行它们时,它们会返回错误:

C:\PROJECT_DIR\foo.test.js
      ...initialState,
      ^^^

SyntaxError: Unexpected token ...

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:318:17)

是什么导致这个?我做了npm install,一切都已安装,但即使在以前的机器上没有出现,我仍然会收到此错误。

这是测试:

import increments from '../increments'

const initialState = {
  goldBase: 0,
  goldMulti: 1,
  goldIncr: 0,
  coalBase: 0,
  coalMulti: 1,
  coalIncr: 0,
  ironBase: 0,
  ironMulti: 1,
  ironIncr: 0,
  stoneBase: 0,
  stoneMulti: 1,
  stoneIncr: 0,
  woodBase: 0,
  woodMulti: 1,
  woodIncr: 0,
  foodBase: 0,
  foodMulti: 1,
  foodIncr: 0
}

describe('increments', () => {
  it('should properly increment values of base increments', () =>  {
    const action = {
      type: 'INCREMENT',
      goldBase: 1,
      coalBase: 0.2,
      ironBase: 0.8,
      stoneBase: NaN,
      woodBase: undefined,
      foodBase: null
    };

    const testState = {
      ...initialState,
      coalBase: 0.1
    };

    const expectedState = {
      ...initialState,
      goldBase: 1,
      coalBase: 0.3,
      ironBase: 0.8,
    };
    expect(increments(testState, action)).toEqual(expectedState);
  });

这是package.json

 {
  "name": "foo",
  "version": "0.1.0",
  "description": "",
  "private": true,
  "dependencies": {
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-redux": "^5.0.6",
    "react-scripts": "1.1.0",
    "redux": "^3.7.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "jest",
    "test:watch": "npm test -- --watch",
    "eject": "react-scripts eject"
  },
  "author": "",
  "license": "MIT",
  "homepage": "",
  "devDependencies": {
    "babel-jest": "^22.1.0",
    "babel-preset-es2015": "^6.24.1",
    "jest": "^22.1.4"
  },
  "babel": {
    "presets": [
      "es2015"
    ]
  }
}

1 个答案:

答案 0 :(得分:0)

所以语法来自transform-object-rest-spread插件,它是babel的一部分(我看到你有),但看起来你没有这个特定的插件。因此,无论JS引擎开玩笑使用什么,都可能不知道如何解释spread(...)语法。

尝试将插件添加到项目中并启用它,然后查看它是否有效!