SyntaxError: Use of reserved word 'import' running enzyme with karma

Deadly 提交于 2019-12-11 14:08:57

问题


Tried unit testing my React application with Enzyme v3, did not work. PFB the details:

modules installed:

"enzyme": "^3.1.1",
"enzyme-adapter-react-15.4": "^1.0.5",

Created a file enzyme.config.js:

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-15.4';

Enzyme.configure({ adapter: new Adapter() });

Included the above file in my karma runner conf:

files: [
        // test setup
        ...........
        'test/unit/testutils/enzyme.config.js',
        ..........
]

Got following error:

14 11 2017 16:21:48.962:INFO [PhantomJS 2.1.1 (Windows 7 0.0.0)]: Connected on socket GWRhv0qSSmqs4UrpAAAC with id 5625410
PhantomJS 2.1.1 (Windows 7 0.0.0) ERROR
  SyntaxError: Use of reserved word 'import'
  at test/unit/testutils/enzyme.config.js:1

Is there anything I am missing?


回答1:


Are you using babel? You should, one way would be using babel-register before runing the tests, and use the following .babelrc file: (install its dependencies)

{
  "presets": ["es2015", "react"],
  "plugins": [
    "transform-es2015-modules-commonjs"
  ]
}

Or install karma-babel-preprocessor

karma.conf:

module.exports = function (config) {
  config.set({
    preprocessors: {
      'src/**/*.js': ['babel'],
      'test/**/*.js': ['babel']
    },
    babelPreprocessor: {
      options: {
        presets: ['env'],
        sourceMap: 'inline'
      },
    ...


来源:https://stackoverflow.com/questions/47284074/syntaxerror-use-of-reserved-word-import-running-enzyme-with-karma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!