mocha / babel 如何即时转换我的测试代码?
How does mocha / babel transpile my test code on the fly?
我的问题不是为什么某些东西不起作用,而是为什么它不起作用。是的
我有一个小的 nodeJS command line tool,其中包含 nodeJS 尚不支持的现成功能,最值得注意的是:
import
语句
String.includes()
.
因此为了交付(构建)我转译+捆绑我的源代码(使用parcel,就像webpack一样)。
作为一个积极的奇迹,我的所有(除了一个)mocha 测试 运行 直接 针对我的 类,而不是包。尽管如此,它们仍然有效!包括许多 import
语句。并包括 'ES6 self-test':
it( 'String - include', () => {
var s = 'Southern Bananas'
assert( s.includes( 'anana' ) )
assert( !s.includes( 'kiwi' ) )
} )
因此:
我的 测试代码 中有 String.include,而不仅仅是在被测源代码中。而且没有地方可以让我转译或捆绑我的测试代码……因此为我的愚蠢问题道歉:
为什么这行得通?某处有秘密的即时编译吗? (如果是,我是否也可以将其用于我的可交付测试代码的调试风格?)
我的mocha.opts
比较简单:
--require @babel/register
--require ./test/once.js (nothing special here, either)
--reporter list
--recursive
我的 .babelrc
有这个:
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"Electron": "3.0",
"Node": "8.0"
}
}
]
],
"plugins": [
"@babel/plugin-transform-runtime"
],
"retainLines": true,
"comments": false,
"sourceMaps": true
}
@babel/plugin-transform-runtime
显然不是责备赞美,因为它explicitly states
NOTE: Instance methods such as "foobar".includes("foo")
will not work since that would require modification of
existing built-ins (you can use @babel/polyfill for that).
@babel/polyfill
是否包含在 minimalistik-modern afaik @babel/preset-env
中?我还有什么做对的:+)?有没有办法将这个实时编译也用于我的(调试)构建?
长话短说
String.prototype.includes
从 v6.5 开始被 Node.js 支持。 @babel/register
导致您的代码被即时编译,这就是您的 import
语句起作用的原因。我怀疑你是否需要 @babel/plugin-transform-runtime
插件,除非我遗漏了你想要实现的东西。
什么会导致这种混乱?
我认为这个(完全可以理解的)谜团有两个根本原因:
- Babel 作者已经真正 易于使用该工具;有时很难知道 how/when 它正在被调用(尤其是与 Mocha 等其他工具配对时)。
- is/isnNode.js 本身不支持的内容(就 ES2015、ES2016 等而言)传统上很难跟上。
所以,关于两个谜团。
为什么 String.prototype.includes
有效?
这个解释更简单。 String.prototype.includes
早在 Node.js v6.5 就得到了原生支持(as you can see,绝大多数 ES2015 支持自该版本以来就已得到支持)。
因此,虽然您没有配置 @babel/polyfill
是正确的(据我所知)并且您 会 需要它不支持的环境 String.prototype.includes
,您的环境已经支持它!
来自 Node.js v8.x 回复:
> 'ES2015'.includes('2015')
true
为什么您的 import
语句有效?
如您所述,Node.js v8.x 本身不支持 ECMAScript 模块。然而,there are some good write ups about how it has been enabled as an experimental feature starting in Node.js v9.x.
因此,您使用本机 Node.js v8.x(通过 REPL)获得以下内容:
> import path from 'path';
import path from 'path';
^^^^^^
SyntaxError: Unexpected token import
你的导入工作的原因是因为你的代码是由 Babel 使用 @babel/preset-env
预设编译的。 此外,编译是由你的 --require @babel/register
摩卡选项。
@babel/register
由 "bind[ing] itself to node's require and automatically compile files on the fly" 创作。
下面是 @babel/register
的基本示例:
从命令行:
$ node main.js
You will see this, because there is no syntax error!
main.js
require('@babel/register');
// This next file is compiled on the fly
require('./file1.js');
file1.js
import path from 'path';
console.log('You will see this, because there is no syntax error!');
好消息是,这就是 Mocha 推荐您集成 Babel 的方式 in their documentation。 --require
选项基本上完成上面示例所做的事情:在 Mocha 使用 require
导入所有测试文件之前调用 require('@babel/register');
。
希望对您有所帮助!同样,在当今快速发展的时代,这是一个完全可以理解的谜团 JavaScript。
我的问题不是为什么某些东西不起作用,而是为什么它不起作用。是的
我有一个小的 nodeJS command line tool,其中包含 nodeJS 尚不支持的现成功能,最值得注意的是:
import
语句String.includes()
.
因此为了交付(构建)我转译+捆绑我的源代码(使用parcel,就像webpack一样)。
作为一个积极的奇迹,我的所有(除了一个)mocha 测试 运行 直接 针对我的 类,而不是包。尽管如此,它们仍然有效!包括许多 import
语句。并包括 'ES6 self-test':
it( 'String - include', () => {
var s = 'Southern Bananas'
assert( s.includes( 'anana' ) )
assert( !s.includes( 'kiwi' ) )
} )
因此:
我的 测试代码 中有 String.include,而不仅仅是在被测源代码中。而且没有地方可以让我转译或捆绑我的测试代码……因此为我的愚蠢问题道歉:
为什么这行得通?某处有秘密的即时编译吗? (如果是,我是否也可以将其用于我的可交付测试代码的调试风格?)
我的mocha.opts
比较简单:
--require @babel/register
--require ./test/once.js (nothing special here, either)
--reporter list
--recursive
我的 .babelrc
有这个:
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"Electron": "3.0",
"Node": "8.0"
}
}
]
],
"plugins": [
"@babel/plugin-transform-runtime"
],
"retainLines": true,
"comments": false,
"sourceMaps": true
}
@babel/plugin-transform-runtime
显然不是责备赞美,因为它explicitly states
NOTE: Instance methods such as "foobar".includes("foo") will not work since that would require modification of existing built-ins (you can use @babel/polyfill for that).
@babel/polyfill
是否包含在 minimalistik-modern afaik @babel/preset-env
中?我还有什么做对的:+)?有没有办法将这个实时编译也用于我的(调试)构建?
长话短说
String.prototype.includes
从 v6.5 开始被 Node.js 支持。 @babel/register
导致您的代码被即时编译,这就是您的 import
语句起作用的原因。我怀疑你是否需要 @babel/plugin-transform-runtime
插件,除非我遗漏了你想要实现的东西。
什么会导致这种混乱?
我认为这个(完全可以理解的)谜团有两个根本原因:
- Babel 作者已经真正 易于使用该工具;有时很难知道 how/when 它正在被调用(尤其是与 Mocha 等其他工具配对时)。
- is/isnNode.js 本身不支持的内容(就 ES2015、ES2016 等而言)传统上很难跟上。
所以,关于两个谜团。
为什么 String.prototype.includes
有效?
这个解释更简单。 String.prototype.includes
早在 Node.js v6.5 就得到了原生支持(as you can see,绝大多数 ES2015 支持自该版本以来就已得到支持)。
因此,虽然您没有配置 @babel/polyfill
是正确的(据我所知)并且您 会 需要它不支持的环境 String.prototype.includes
,您的环境已经支持它!
来自 Node.js v8.x 回复:
> 'ES2015'.includes('2015')
true
为什么您的 import
语句有效?
如您所述,Node.js v8.x 本身不支持 ECMAScript 模块。然而,there are some good write ups about how it has been enabled as an experimental feature starting in Node.js v9.x.
因此,您使用本机 Node.js v8.x(通过 REPL)获得以下内容:
> import path from 'path';
import path from 'path';
^^^^^^
SyntaxError: Unexpected token import
你的导入工作的原因是因为你的代码是由 Babel 使用 @babel/preset-env
预设编译的。 此外,编译是由你的 --require @babel/register
摩卡选项。
@babel/register
由 "bind[ing] itself to node's require and automatically compile files on the fly" 创作。
下面是 @babel/register
的基本示例:
从命令行:
$ node main.js
You will see this, because there is no syntax error!
main.js
require('@babel/register');
// This next file is compiled on the fly
require('./file1.js');
file1.js
import path from 'path';
console.log('You will see this, because there is no syntax error!');
好消息是,这就是 Mocha 推荐您集成 Babel 的方式 in their documentation。 --require
选项基本上完成上面示例所做的事情:在 Mocha 使用 require
导入所有测试文件之前调用 require('@babel/register');
。
希望对您有所帮助!同样,在当今快速发展的时代,这是一个完全可以理解的谜团 JavaScript。