简体   繁体   English

开玩笑如何测试 map 内容,即键和值

[英]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.

相关问题 如何查询 DynamoDB 中任何项目的 GSI 范围(即不依赖于分区键)? - How do I query the range of a GSI of any item in DynamoDB (i.e. not partition key dependent)? 如何在测试(开玩笑)中手动设置变量的值? - How do I manually set the value of a variable in a test (jest)? 如何处理(即记录)纠正错误 - How to handle (i.e. log) errors in restify 如何通过查询参数在 mongoose 中创建增量范围聚合。 即从字段值创建增量范围 - How to create incremental range aggregation in mongoose from a query parameter. i.e. create an incremental range from a field value 如何转换一组值,使每个值更接近平均值,但在 PySpark 中具有类似形状的分布(即减少 stdev) - How to convert an array of values so that each value is closer the mean, but with a similarly shaped distribution (i.e. reduce the stdev) in PySpark 我如何测试(开玩笑)以期待回报? - How do I test (jest) to expect return? 如何使用 Jest 测试身份验证中间件 - How can I test authentication middleware with Jest 如何在 Jest 上测试节点 cron - How can I test node cron on Jest 如何使用 Jest 测试 object - How can I test an object using Jest 如何使用 Jest 测试 JS function? - How would I test a JS function with Jest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM