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

Nic*_*ady 10 javascript unit-testing ecmascript-6 reactjs yarnpkg

我有我的一个组件的方法一些代码使用flatMap。该代码在浏览器中工作正常,但是flatMap使用来运行代码时该方法不存在yarn test。以前有没有人看过这样的东西?

? 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));
      |        ^
Run Code Online (Sandbox Code Playgroud)

编辑-可重现的示例:创建了CRA库并添加了以下简单测试:

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

有同样的错误:

 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
Run Code Online (Sandbox Code Playgroud)

Nic*_*ady 15

我的一个同事找到了解决方案。显然flatMap,Node.js不支持:

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


flatMap 如下所述,现在在节点11+中受支持,请参阅同一链接。


Dre*_*ese 5

值得一提的是,使用 react polyfill 包也对我有用。我可以控制我自己的本地环境,但我必须使用的构建系统受到更多团队的限制和使用。

npm 包: react-app-polyfill

用法:

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

这适用于我目前拥有的 Node v10.16.0。