首页 > 解决方案 > 运行 mocha 测试时出现意外的令牌“{”

问题描述

我创建了一个反应应用程序,我想使用 mocha/chai/enzyme 组合进行测试,但是我在设置测试文件时遇到了麻烦。

我决定遵循一个创建 mocha 测试的教程,可以在这里找到:https ://www.robinwieruch.de/react-testing-mocha-chai-enzyme-sinon 。我已经到了创建使用 mocha 命令的脚本的地步。以下

这是我的 package.json:

{
  "name": "congressforme-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/core": "^7.6.4",
    "@babel/register": "^7.6.2",
    "@types/react": "^16.9.4",
    "@types/react-bootstrap": "^0.32.20",
    "@types/react-dom": "^16.9.1",
    "@types/react-router-bootstrap": "^0.24.5",
    "@types/react-router-dom": "^5.1.0",
    "axios": "^0.19.0",
    "bootstrap": "^4.3.1",
    "bootstrap-3-card": "^0.2.0",
    "chai": "^4.2.0",
    "enzyme": "^3.10.0",
    "enzyme-adapter-react-16": "^1.15.1",
    "ignore-styles": "^5.0.1",
    "jquery": "^3.4.1",
    "jsdom": "^15.2.0",
    "mdbreact": "^4.21.0",
    "mocha": "^6.2.2",
    "newman": "^4.5.5",
    "popper.js": "^1.15.0",
    "react": "^16.10.1",
    "react-bootstrap": "^1.0.0-beta.12",
    "react-bootstrap-card": "^0.2.1",
    "react-dom": "^16.9.0",
    "react-paginate": "^6.3.0",
    "react-router-bootstrap": "^0.25.0",
    "react-router-dom": "^4.3.1",
    "react-scripts": "3.1.2",
    "react-twitter-embed": "^2.0.8",
    "typescript": "^3.6.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "deploy": "aws s3 sync build/ s3://congressforme",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "my-tests": "mocha --require @babel/register --require ./test/helpers.js --require ./test/dom.js --require ignore-styles ./src/**/*.spec.js"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

这是我收到的错误消息:

ERROR: Unexpected token {
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! congressforme-app@0.1.0 my-tests: `mocha --require `@babel/register --require ./test/helpers.js --require ./test/dom.js --require ignore-styles ./src/**/*.spec.js

我的 src 文件夹中只有 1 个测试文件。它被命名为 App.spec.js

这是它的样子:

import React from 'react';
import App from './App';
import  Navbar  from "react-bootstrap";

// this is a testing suite app component
describe('App Component', () => {

  // this is a single test in the testing suite
  it('renders the Counter wrapper', () => {
    const wrapper = shallow(<App />);
    expect(wrapper.find(Navbar)).to.have.length(1);
  });
});

但是,即使我完全删除 App.spec.js 的内容,仍然会出现同样的错误。

标签: javascriptreactjsmocha.jsenzymechai

解决方案


推荐阅读