Jest遇到了意外的令牌:SyntaxError:Unexpected Token { [英] Jest encountered an unexpected token: SyntaxError: Unexpected Token {

查看:1251
本文介绍了Jest遇到了意外的令牌:SyntaxError:Unexpected Token {的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在另一台机器上运行代码时遇到此错误时,我正在使用Jest和Enzyme运行我的测试。

I am running my tests with Jest and Enzyme when I came across this error when I was running my code on a different machine.

当我运行 npm test 时,只运行 jest --coverage --verbose 每次都会出现此错误。但是,当我在我的工作圈上运行时,这不会发生。

When I run npm test which only runs jest --coverage --verbose it will get this error every time. However, this doesn't happen when I run it on my work lap top.

我想弄清楚的是为什么当我运行我的测试脚本时,它会突破{标记。我见过一些人建议使用babel-jest会有所帮助,但我不相信这是导致这个问题,因为它不会影响我的笔记本电脑工作。我还读到,babel-jest无法解决这个问题。

What I'm trying to figure out is why this is breaking over the "{" token when I run my testing script. I've seen some people suggest that using babel-jest would help, but I don't believe that is the cause of this issue, as it doesn't affect my work laptop. I've also read that babel-jest doesn't resolve this issue.

任何协助都将不胜感激。

Any assitance would be greatly appreciated.

setUptests.js

import { configure } from 'enzyme'; // eslint-disable-line import/no-extraneous-dependencies

import Adapter from 'enzyme-adapter-react-16'; // eslint-disable-line import/no-extraneous-dependencies

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

我的 webpack.config.js

const path = require('path');

module.exports = {
  entry: {
    base: path.resolve(__dirname, './app/app.jsx'),
    redux: path.resolve(__dirname, './app/reduxApp.jsx'),
  },
  output: {
    path: path.resolve(__dirname, './public'),
    filename: '[name].bundle.js',
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
          presets: ['env', 'react', 'stage-2'],
        },
      },
    ],
  },
  resolve: {
    extensions: ['.js', '.jsx'],
  },
};

我的 package.json 包含配置我在这里开玩笑:

My package.json has the configurations for my jest here:

"jest": {
    "testURL": "http://localhost/",
    "setupTestFrameworkScriptFile": "./setUpTests.js",
    "testEnvironment": "node",
    "unmockedModulePathPatterns": [
      "node_modules/react/",
      "node_modules/enzyme/"
    ],
    "coverageThreshold": {
      "global": {
        "statements": 98,
        "branches": 91,
        "functions": 98,
        "lines": 98
      }
    }
  }

以下是我正在使用的依赖项列表:

and here are the list of dependencies that I'm using:

  "dependencies": {
    "express": "^4.16.3",
    "prop-types": "^15.6.2",
    "react": "^16.4.2",
    "react-dom": "^16.4.2",
    "react-redux": "^5.0.7",
    "redux": "^4.0.0"
  },
  "devDependencies": {
    "babel": "^6.23.0",
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "enzyme": "^3.4.1",
    "enzyme-adapter-react-16": "^1.2.0",
    "eslint": "^4.19.1",
    "eslint-config-airbnb": "^17.0.0",
    "eslint-plugin-import": "^2.13.0",
    "eslint-plugin-jsx-a11y": "^6.1.1",
    "eslint-plugin-react": "^7.10.0",
    "jest": "^23.4.2",
    "jsdom": "^11.12.0",
    "nodemon": "^1.18.3",
    "react-test-renderer": "^16.4.2",
    "webpack": "^4.16.5",
    "webpack-cli": "^3.1.0"
  }






更新:



尝试切换代码:
从'酶'导入{configure};
到:
从'酶'进口酶;

我是现在假设它是一个进口问题。将继续研究,看看会解决这个问题。

I'm assuming its an import issue at the moment. Will continue to research to see what will fix this.

推荐答案

尝试将以下插件添加到 .babelrc 配置文件中以修复 import Enzyme 错误:

Try to add the following plugin to your .babelrc config file in order to fix the import Enzyme error:

plugins:[transform-es2015-modules-commonjs]

这篇关于Jest遇到了意外的令牌:SyntaxError:Unexpected Token {的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆