【问题标题】:SyntaxError: Use of reserved word 'import' running enzyme with karmaSyntaxError:使用保留字“导入”运行酶与业力
【发布时间】:2023-03-28 06:53:01
【问题描述】:

尝试使用 Enzyme v3 对我的 React 应用程序进行单元测试,但没有成功。 PFB详情:

已安装的模块:

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

创建了一个文件enzyme.config.js

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

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

在我的 karma runner conf 中包含上述文件:

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

出现以下错误:

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

我有什么遗漏吗?

【问题讨论】:

  • 我不确定 PhantomJs 是否理解 import - 也许尝试使用 require 导入来看看它是否有所作为?

标签: reactjs gruntjs requirejs karma-runner enzyme


【解决方案1】:

你在使用 babel 吗?您应该,一种方法是在运行测试之前使用 babel-register,并使用以下 .babelrc 文件:(安装其依赖项)

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

或者安装karma-babel-preprocessor

karma.conf:

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

【讨论】:

    猜你喜欢
    • 2016-10-07
    • 2014-01-10
    • 2017-06-09
    • 2012-02-19
    • 2022-06-10
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多