其他相关问题中提供的解决方案,例如在.babelrc中包含正确的预设(es2015),已在我的项目中实现 .
我有两个项目(让我们称之为A和B),它们都使用ES6模块语法 . 在项目A中,我正在导入通过npm安装的项目B,它位于node_modules文件夹中 . 当我为项目A运行我的测试套件时,我收到错误:
SyntaxError:意外的令牌导入
之前是项目B的这个错误的代码行:
(function(exports,require,module,__ filename,__ dirname){从'history / lib / createBrowserHistory'导入createBrowserHistory;
因为我的源文件只包含“来自'history / lib / createBrowserHistory'的import createBrowserHistory',所以iife似乎是npm或者babel相关的东西; Project B的测试套件中的单元测试运行正常,如果我将Project B作为依赖项从项目A,我的测试套件(仍然使用es6导入内部项目模块)工作正常 .
完整堆栈跟踪:
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中的测试命令:
"test": "mocha --compilers js:babel-core/register '+(test|src)/**/*Spec.js'"
此StackOverflow帖子类似,但不提供我使用命令行的解决方案:import a module from node_modules with babel but failed
14 回答
对于使用Babel 7和Mocha 4的任何人来说,一些软件包名称已经改变了一些,并且接受的答案有点过时了 . 我必须做的是:
npm install @babel/register --saveDev
并在
package.json
中将--require @babel/register
添加到测试行"test": "./node_modules/mocha/bin/mocha --require @babel/polyfill --require @babel/register './test/**/*.spec.js'"
@babel/polyfill
修复了一些事情,比如async / await功能,如果你正好使用的话那些 .希望能帮助别人:)
巴别<6
解决此问题的最简单方法是:
npm install babel-preset-es2015 --save-dev
将
.babelrc
添加到项目的根目录中,其中包含以下内容:确保使用“--compilers js:babel-core / register”参数运行mocha .
对于Babel6 / 7
npm install @babel/preset-env --save-dev
将
.babelrc
添加到项目的根目录中,其中包含以下内容:确保使用“--compilers js:babel-register”参数运行mocha
似乎唯一的解决方案是明确包括:
在测试助手文件中,并在我的测试命令中将其传递给mocha:
The final solution:
添加 registerBabel.js :一个单独的文件,其工作是要求babel-core / register ...
如果您的应用程序也依赖于babel-node,请添加 entry.js . 这充当了包含es6的应用程序的包装器 .
然后,您将使用
node entry
运行您的应用程序对于mocha测试, testHelper.js 也应该要求registerBabel.js在运行时初始化babel支持 .
用
mocha --require ./testHelper.js '+(test)/**/*Spec.js'
运行你的摩卡测试这将递归测试在“./test”中以“Spec.js”结尾的任何文件 . 用符合项目规格的模式替换模式 .
确定你会遇到这个问题,你使用的是摩卡不知道的ES6
所以你使用的是babel,但你没有在测试中使用它...
几个解决方案:
A.如果你使用NPM运行
我正在使用
C.来自cli:
你可以在http://www.pauleveritt.org/articles/pylyglot/es6_imports/阅读更多内容
我遇到了同样的问题 . 在stackoverflow及其他方面尝试了所有其他解决方案后,在package.json上添加这个简单的配置为我做了:
之后我所有的ES6进口都有效 . 顺便说一句,我在webpack.config.js中有相同的配置,但显然这是使其适用于mocha测试的唯一方法 .
希望这有助于某人 .
我的.babelrc文件中有
{"modules": false}
,如下所示:这是投掷
一旦我删除它,摩卡成功运行 .
我有同样的问题并通过阅读babel documentation来修复它,以便将Babel与Mocha整合:
我找到最简单的方法与babel 6.X.X一起使用nyc,然后将
helper
文件添加到pckage.json
中所以这就是我用过的东西
package.json
babelrc
registerBabel.js
现在,您可以在测试中或任何需要的地方使用es6 . 我的都失败了;)
然后
npm run test
将发射nyc mocha --reporter tap 'test/**/*.spec.js'
--compilers
已弃用 .我的简单方案:
在package.json中添加你的测试脚本如下:
这对我有用 . 使用
--compilers
标志时我收到了警告 .因此,我用
--require
标志取而代之这是我的
.babelrc
:我的
package.json
dev依赖项今天早上我按照来自摩卡4的official Using Babel instructions的说明解决了这个问题:
Install NPM Modules
或单个命令:
package.json :
.babelrc
我在我的代码中使用
import
时安装了mocha
并遇到了完全相同的错误 . 通过执行以下操作,问题得以解决 .然后添加
.babelrc
文件:然后以这种方式运行
mocha
:我有同样的问题 . 在运行测试时,我意识到我实际上想要存根依赖模块 . 它有利于单元测试并防止babel转换子模块 . 所以我使用
proxyquire
,即:为了更加面向未来的设置
在.babelrc
而不是......
和