简体   繁体   English

flatMap 在纱线测试 javascript 中不起作用

[英]flatMap doesn't work in yarn test javascript

I have some code in a component method of mine using flatMap .使用flatMap在我的组件方法中有一些代码。 The code works just fine in the browser, but the flatMap method isn't there when running the code using yarn test .代码在浏览器中工作得很好,但在使用yarn test运行代码时, flatMap方法不存在。 Has anyone ever seen something like this before?有没有人见过这样的事情?

● Click Events › should copy table data

TypeError: children.map(...).flatMap is not a function

  181 |     const dataKeys = children
  182 |       .map(({ $: { data: dataKey } }) => dataKey)
> 183 |       .flatMap(k => k.split(LEVEL_DELIMITER));
      |        ^

EDIT - reproducible example: created a CRA base and added this simple test:编辑 - 可重现的示例:创建了一个 CRA 基础并添加了这个简单的测试:

it('can flatMap', () => {
    [1, 2, 3, 4].flatMap(x => [x * 2]);
});

got the same error:得到了同样的错误:

 FAIL  src/App.test.js
● can flatMap

TypeError: [1,2,3,4].flatMap is not a function

  at Object.<anonymous>.it (src/App.test.js:12:16)
      at new Promise (<anonymous>)
  at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

✓ renders without crashing (4ms)
✕ can flatMap

A coworker of mine found the solution. 我的一位同事找到了解决方案。 Apparently flatMap isn't supported by Node.js: 显然,Node.js不支持flatMap

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap#Browser_compatibility https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap#Browser_compatibility


flatMap is now supported in node 11+ as said below, see the same link. 现在,节点11+支持flatMap ,如下所述,请参阅相同的链接。

For what it is worth, using a react polyfill package also worked for me.值得一提的是,使用 react polyfill 包也对我有用。 I can control my own local environment, but the build system I have to work with is more restricted and used by multiple teams.我可以控制我自己的本地环境,但我必须使用的构建系统受到更多团队的限制和使用。

npm package: react-app-polyfill npm 包: react-app-polyfill

Usage:用法:

// setupTests.js (jest)
// Using ie9 polyfills as the "kitchen sink" of polyfills
import 'react-app-polyfill/ie9';
import 'react-app-polyfill/stable';

This works for Node v10.16.0, what I have currently.这适用于我目前拥有的 Node v10.16.0。

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

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