[英]flatMap doesn't work in yarn test javascript
我使用flatMap
在我的組件方法中有一些代碼。 代碼在瀏覽器中工作得很好,但在使用yarn test
運行代碼時, flatMap
方法不存在。 有沒有人見過這樣的事情?
● 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));
| ^
編輯 - 可重現的示例:創建了一個 CRA 基礎並添加了這個簡單的測試:
it('can flatMap', () => {
[1, 2, 3, 4].flatMap(x => [x * 2]);
});
得到了同樣的錯誤:
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
我的一位同事找到了解決方案。 顯然,Node.js不支持flatMap
:
flatMap
,如下所述,請參閱相同的鏈接。
值得一提的是,使用 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';
這適用於我目前擁有的 Node v10.16.0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.