gpt4 book ai didi

node.js - mocha/babel 如何即时转换我的测试代码?

转载 作者:搜寻专家 更新时间:2023-10-31 23:44:30 26 4
gpt4 key购买 nike

我的问题不是为什么某些东西不起作用,而是为什么它起作用了。是的。

我有一个小nodeJS command line tool ,其中包含 nodeJS 尚不支持开箱即用的功能,最值得注意的是:

  • import 语句
  • String.includes()

因此,为了交付(构建),我转译+捆绑我的源代码(使用 parcel ,就像 webpack 一样)。

作为一个积极的奇迹,我的所有(除了一个)mocha 测试直接针对我的类运行,而不是包。尽管如此,它们仍然有效!包括许多 import 语句。并包括“ES6 自测”:

it( 'String - include', () => {
var s = 'Southern Bananas'
assert( s.includes( 'anana' ) )
assert( !s.includes( 'kiwi' ) )
} )

因此:

我的测试代码 中有 String.include,而不仅仅是在被测源代码中。并且没有地方可以让我转译或捆绑我的测试代码……因此为我的愚蠢问题道歉:

为什么会这样?某处有 secret 的即时编译吗? (如果是,我是否也可以将其用于我的可交付测试代码的调试风格?)

我的 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 插件,除非我遗漏了你想要实现的东西。

是什么导致了这种困惑?

我认为这个(完全可以理解的)谜团有两个根本原因:

  1. Babel 的作者让这个工具真的易于使用;有时很难知道它是如何/何时被调用的(尤其是与 Mocha 等其他工具配对时)。
  2. Node.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 REPL:

> 'ES2015'.includes('2015')
true

为什么您的 import 语句有效?

如您所述,Node.js v8.x 本身不支持 ECMAScript 模块。然而,there are some good write ups关于如何将其作为实验性功能启用 starting in Node.js v9.x .

因此,您可以使用 native Node.js v8.x(通过 REPL)获得以下内容:

> import path from 'path';
import path from 'path';
^^^^^^

SyntaxError: Unexpected token import

您的导入工作的原因是因为您的代码正在由 Babel 使用 @babel/preset-env 预设编译。此外,该编译是由你的 --require @babel/register Mocha 选项。

@babel/register通过“将自身绑定(bind)到 Node 的要求并自动即时编译文件”来工作。

下面是 @babel/register 的一个基本例子:

从命令行:

$ node main.js
You will see this, because there is no syntax error!

主要.js

require('@babel/register');
// This next file is compiled on the fly
require('./file1.js');

文件1.js

import path from 'path';
console.log('You will see this, because there is no syntax error!');

好的是,这就是 Mocha 推荐您集成 Babel 的方式 in their documentation . --require 选项基本上做了上面例子做的事情:require('@babel/register'); 在 Mocha 使用 require 之前被调用导入所有测试文件。

希望对您有所帮助!同样,在 JavaScript 快速发展的现代时代,这是一个完全可以理解的谜团。

关于node.js - mocha/babel 如何即时转换我的测试代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53874360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com