运行 mocha 测试时 Babel 意外的令牌导入 [英] Babel unexpected token import when running mocha tests

查看:25
本文介绍了运行 mocha 测试时 Babel 意外的令牌导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其他相关问题中提供的解决方案,例如在 .babelrc 中包含适当的预设 (es2015),已在我的项目中实现.

The solutions offered in other related questions, such as including the proper presets (es2015) in .babelrc, are already implemented in my project.

我有两个项目(我们称它们为 A 和 B),它们都使用 ES6 模块语法.在项目 A 中,我正在导入通过 npm 安装并位于 node_modules 文件夹中的项目 B.当我为项目 A 运行测试套件时,出现错误:

I have two projects (lets call them A and B) which both use ES6 module syntax. In Project A, I'm importing Project B which is installed via npm and lives in the node_modules folder. When I run my test suite for Project A, I'm getting the error:

语法错误:意外的令牌导入

SyntaxError: Unexpected token import

前面是项目 B 中这段所谓的错误代码行:

Which is preceded by this alleged erroneous line of code from Project B:

(function (exports, require, module, __filename, __dirname) { importcreateBrowserHistory from 'history/lib/createBrowserHistory';

(function (exports, require, module, __filename, __dirname) { import createBrowserHistory from 'history/lib/createBrowserHistory';

iife 似乎与 npm 或可能与 babel 相关,因为我的源文件只包含import createBrowserHistory from 'history/lib/createBrowserHistory';项目 B 的测试套件中的单元测试运行良好,如果我将项目 B 删除为项目 A 的依赖项,我的测试套件(仍然使用 es6 导入内部项目模块)工作正常.

The iife appears to be something npm or possibly babel related since my source file only contains "import createBrowserHistory from 'history/lib/createBrowserHistory'; The unit tests in Project B's test suite runs fine, and if I remove Project B as a dependency from Project A, my test suite then (still using es6 imports for internal project modules) works just fine.

完整的堆栈跟踪:

 SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:374:25)
    at Module._extensions..js (module.js:405:10)
    at Object.require.extensions.(anonymous function) [as .js] (/ProjectA/node_modules/babel-register/lib/node.js:138:7)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (actionCreators.js:4:17)
    at Module._compile (module.js:398:26)
    at loader (/ProjectA/node_modules/babel-register/lib/node.js:130:5)
    at Object.require.extensions.(anonymous function) [as .js] (/ProjectA/node_modules/babel-register/lib/node.js:140:7)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/ProjectA/src/components/core/wrapper/wrapper.js:28:23)
    at Module._compile (module.js:398:26)
    at loader (/ProjectA/node_modules/babel-register/lib/node.js:130:5)
    at Object.require.extensions.(anonymous function) [as .js] (/ProjectA/node_modules/babel-register/lib/node.js:140:7)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/ProjectA/src/components/core/wrapper/wrapperSpec.js:15:16)
    at Module._compile (module.js:398:26)
    at loader (/ProjectA/node_modules/babel-register/lib/node.js:130:5)
    at Object.require.extensions.(anonymous function) [as .js] (/ProjectA/node_modules/babel-register/lib/node.js:140:7)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at /ProjectA/node_modules/mocha/lib/mocha.js:219:27
    at Array.forEach (native)
    at Mocha.loadFiles (/ProjectA/node_modules/mocha/lib/mocha.js:216:14)
    at Mocha.run (/ProjectA/node_modules/mocha/lib/mocha.js:468:10)
    at Object.<anonymous> (/ProjectA/node_modules/mocha/bin/_mocha:403:18)
    at Module._compile (module.js:398:26)
    at Object.Module._extensions..js (module.js:405:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:430:10)
    at startup (node.js:141:18)
    at node.js:980:3

这是我来自 package.json 的测试命令:

Here is my test command from package.json:

"test": "mocha --compilers js:babel-core/register '+(test|src)/**/*Spec.js'"

这篇 StackOverflow 帖子很相似,但没有为我使用命令行提供解决方案:使用 babel 从 node_modules 导入模块但失败

This StackOverflow post is similar but doesn't offer a solution for my use of the command line: import a module from node_modules with babel but failed

推荐答案

似乎唯一的解决方案是明确包含:

It seems the only solution is to explicitly include:

require('babel-core/register')({
  ignore: /node_modules/(?!ProjectB)/
}); 

在测试帮助文件中,并在我的测试命令中将其传递给 mocha:

in a test helper file, and pass that along to mocha in my test command:

mocha --require ./test/testHelper.js...

<小时>

最终解决方案:

添加 registerBabel.js:一个单独的文件,其工作是需要 babel-core/register...

Add registerBabel.js: a separate file whose job is to require babel-core/register...

require('babel-core/register')({
  ignore: /node_modules/(?!ProjectB)/
});

如果您的应用程序还依赖于 babel-node,请添加 entry.js.这充当了包含 es6 的应用程序的包装器.

Add an entry.js if your application also relies on babel-node. This acts as a wrapper for your es6 containing application.

require('./registerBabel');
require('./server'); // this file has some es6 imports

然后您将使用 node entry

对于 mocha 测试,testHelper.js 还应该需要 registerBabel.js 以在运行时初始化 babel 支持.

For mocha testing, testHelper.js should require registerBabel.js as well to initialize babel support at run time.

require('./registerBabel');

并使用 mocha --require ./testHelper.js '+(test)/**/*Spec.js'

这将递归测试./test"中以Spec.js"结尾的任何文件.用与您项目中的规格匹配的模式替换该模式.

This will recursively test any file ending in "Spec.js" within "./test". Substitute the pattern with one matching the specs in your project.

这篇关于运行 mocha 测试时 Babel 意外的令牌导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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