I have an JSON object which has a map as follows:
{"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. It is failing when I use the isEqual([["id", "123"....]) syntax. 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?
This
[["id", "123"], ["status", "active"]]
is an array of arrays, not a 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
.
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). Then that structure can be deep checked with 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');