意外的令牌导入,无法在我的Mocha,Chai和Sinon的JS项目中设置ES6 [英] Unexpected token import, can not set up ES6 in my JS project with Mocha, Chai and Sinon

查看:102
本文介绍了意外的令牌导入,无法在我的Mocha,Chai和Sinon的JS项目中设置ES6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个简单的ES6项目,以便在终端中与摩卡,柴和锡农一起玩.但是每当我尝试npm run test时,我都会不断收到错误消息:

I am trying to set up a simple ES6 project to play with mocha, chai and sinon in the Terminal. But whenever I try to npm run test, I keep getting an error:

/Users/UserName/workspace/UnitTesting/mocha-sinon-chai/src/App.spec.js:1
(function (exports, require, module, __filename, __dirname) { import should from "chai";
                                                          ^^^^^^
SyntaxError: Unexpected token import

这是我的:

1. Package.json:

{
  "name": "mocha-sinon-chai",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "directories": {
    "test": "tests"
  },
  "dependencies": {
    "chai": "^4.1.2",
    "chai-as-promised": "^7.1.1",
    "sinon": "^4.4.1"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "mocha": "^5.0.1"
  },
  "scripts": {
    "build": "babel src -d lib",
    "test": "mocha ./tests/*.spec.js"
  },
  "author": "",
  "license": "ISC"
}

2. .babelrc:

{
  "presets": ["env"]
}

3.根文件夹结构:

4. App.spec.js:

import should from "chai";
import { findOne } from "../src/App.js";

describe("App.js", function() {
  it("should return true if predicate exists in the array", function() {
    findOne([1, 2, 3], 3).should().be.true;
  });
});

5. index.js 为空
6. App.js 仅包含普通的JS函数

5. index.js is empty
6. App.js simply contains a normal JS function

我已经在Stackoverflow和GitHub上检查了许多其他类似的问题,但没有一个解决了我的问题.

I have checked many other similar issues here on Stackoverflow and GitHub and none of those solved my problem.

推荐答案

添加此--require babel-core/register来测试package.json中的脚本可以解决此问题:

Adding this --require babel-core/register to test script inside package.json solved the problem:

"scripts": {
    "build": "babel src -d lib",
    "test": "mocha ./tests/*.spec.js --require babel-core/register"
  },

这篇关于意外的令牌导入,无法在我的Mocha,Chai和Sinon的JS项目中设置ES6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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