开玩笑Enzyme - SyntaxError:意外的令牌导入

时间:2018-03-16 12:10:18

标签: reactjs installation jestjs enzyme

使用Jest& amp;为我的React App编写测试由于我无法与Jest一起设置酶,所以酶对我很有帮助。

以下是我在测试文件import React from 'react';

中使用此行时出现的错误
FAIL  src\App.test.js
  ● Test suite failed to run

    D:\experiements\react\my-app\src\App.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token import

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.049s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

的package.json

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "bootstrap-daterangepicker": "^2.1.30",
    "history": "^4.7.2",
    "jquery": "^3.3.1",
    "moment": "^2.21.0",
    "npm": "^5.7.1",
    "prop-types": "^15.6.0",
    "react": "^16.2.0",
    "react-bootstrap-daterangepicker": "^4.1.0",
    "react-dom": "^16.2.0",
    "react-redux": "^5.0.6",
    "react-router-dom": "^4.2.2",
    "react-toastify": "^3.3.4",
    "redux": "^3.7.2",
    "redux-form": "^7.2.3",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.2.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "jest",
    "eject": "react-scripts eject"
  },
  "homepage": "http://localhost/my-app",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-jest": "^23.0.0-alpha.0",
    "enzyme": "^3.3.0",
    "jest": "20.0.4",
    "jest-cli": "20.0.4",
    "jest-config": "20.0.4",
    "jest-environment-node": "20.0.3",
    "react-scripts": "1.1.0"
  }
}

App.test.js

import React from 'react';

describe("<App />", () => {
    it("renders App Component", () => {
        expect(true).toBe(true);
    })
})

但是,如果我删除import语句并运行'npm test'就行了。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:4)

好像你没有用Babel编译。

如果您安装或更新babel-jest,那么应解决您的问题:

npm install --save-dev babel-jest

这是yarn版本:

yarn add -D babel-jest