[英]Jest how to test a map contents i.e. key and value
I have an JSON object which has a map as follows:我有一个 JSON object 有一个 map 如下:
{"name": "Bertha", [["id", "123"], ["status", "active"]]}
Please note that I am representing the map like it would be represented in typescript but I don't know how it would be represented in the underlying Jest framework.请注意,我代表的是 map,就像它在 typescript 中一样,但我不知道它将如何在底层 Jest 框架中表示。 It is failing when I use the isEqual([["id", "123"....]) syntax.
当我使用 isEqual([["id", "123"....]) 语法时它失败了。 It is reporting that it received:
它报告说它收到了:
Map {"id" => "123", "status" => "active"} which I find quite confusing.
How do I test the key, value pairs using Jest?如何使用 Jest 测试键值对?
This这个
[["id", "123"], ["status", "active"]]
is an array of arrays, not a Map.是 arrays 的数组,而不是 Map。
This这个
Map {"id" => "123", "status" => "active"}
is the conventional syntax for a Map, which would be constructed by passing the array of arrays above into new Map
.是 Map 的常规语法,可以通过将上面的 arrays 数组传递给
new Map
来构造。
Since it looks like what you actually have is a Map, not an array of arrays - to test that the map's key-values are as above, I'd take the Map's entries (to turn it from a Map back into an array of arrays).由于看起来您实际上拥有的是 Map,而不是 arrays 的数组 - 为了测试地图的键值是否如上,我将获取地图的条目(将其从 Z46F3EA056CAA3128B913 的数组转换回数组)。 Then that structure can be deep checked with
toEqual
.然后可以使用
toEqual
深入检查该结构。
const entries = [...map.entries()];
expect(entries).toEqual([["id", "123"], ["status", "active"]]);
The other option (which wouldn't be very sustainable if you had many items in the Map) would be to check each one individually.另一种选择(如果地图中有很多项目,这将不是很可持续)是单独检查每个项目。
expect(map.get('id')).toBe(123);
expect(map.get('status')).toBe('active');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.