Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for Map object keys in Jest

Using Jest, I am trying to check for keys in a JavaScript Map object and toHaveProperty does not work. In Mocha/Chai you can assert.hasAllKeys(map, keys)

JavaScript Map: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

Is there a way to do this?

like image 804
Cazineer Avatar asked Nov 04 '18 02:11

Cazineer


1 Answers

toHaveProperty is meant for checking paths on a plain javascript object. There aren't any built in matchers (that i'm aware of) that specialize in Map objects, so i'd recommend doing something like this:

expect(myMapObject.has(key)).toEqual(true)
like image 99
Nicholas Tower Avatar answered Oct 06 '22 17:10

Nicholas Tower