javascript - flatMap 在纱线测试 javascript 中不起作用
问题描述
我在我的组件方法中有一些代码,使用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));
| ^
编辑 - 可重现的示例:创建了一个 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
现在在节点 11+ 中得到支持,如下所述,请参见相同的链接。
推荐阅读
- javascript - EmberJS:在计算属性中观察 JavaScript 对象的 getter
- java - 当枚举在类中时,在枚举构造函数中创建类的新实例
- html - 对于某些颜色,HTML5 渐变无法正常工作
- mysql - 单个属性用作多个表的外键
- python - 在 Python 中使用某些文本抓取 HTML 表格
- r - 将日期和时间的变量分成两个单独的变量,一个日期和另一个时间
- google-chrome - 在托管模式下不启用保存文件。请使用 chrome://inspect 检查
- django - Model 有多个 ForeignKey,ForeignKey 可以多个一个 Model
- java - Spliterator vs Stream.Builder
- javascript - 在没有 Datepicker 的情况下限制“html 日期字段”中的日期