简体   繁体   English

Array.prototype.flatMap() JS 在测试用例和 repl.it 片段中表现异常,但在浏览器控制台中没有

[英]Array.prototype.flatMap() JS is behaving strangely in test cases and a repl.it snippet, but not in browser console

My apologies if this is a rookie question, but I've stumbled upon a puzzle.如果这是一个菜鸟问题,我很抱歉,但我偶然发现了一个难题。

Why would a JS standard function work in the chrome browser console, but not in an electron react project jest test case, or even in a repl.it snippet?为什么 JS 标准函数可以在 chrome 浏览器控制台中工作,而不是在电子反应项目 jest 测试用例中,甚至在 repl.it 片段中? Could it be a chromium thing?会不会是铬的东西?

The tests were passing last time I ran them a couple of weeks ago (back in 2019) The code affecting the tests hasn't been touched, (I'm fairly certain), but the project has been rebuilt recently.几周前我上次运行测试时(回到 2019 年)测试通过了影响测试的代码没有被触及,(我相当确定),但该项目最近已经重建。

This is the simple example from MDN这是来自MDN的简单示例

 let arr1 = [1, 2, 3, 4]; arr1.flatMap(x => [x * 2]); // [2, 4, 6, 8] console.log(arr1.flatMap(x => [x * 2]));

If you run the above, you will see the the SO compiler is fine with it.如果你运行上面的,你会看到 SO 编译器可以正常运行。

BUT if I access it from a jest test case for an electron react app, it kerplunks:但是如果我从一个电子反应应用程序的笑话测试用例访问它,它会:

在此处输入图片说明

It works fine in the browser console:它在浏览器控制台中工作正常:

在此处输入图片说明

But it does not work in a repl .it snippet:但它在 repl .it 片段中不起作用:

在此处输入图片说明

My guess would be that it is some kind of chromium thing.我的猜测是它是某种铬的东西。 But I'm not sure how to confirm that.但我不确定如何确认。

Could somebody please direct me to the correct resource where I could learn more about this?有人可以将我引导到正确的资源,在那里我可以了解更多信息吗?

因为测试使用的是你电脑上安装的 node.js 版本,升级到第 12 个版本,你的版本中还没有提供 flatMap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM