Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest assertion - object containing a key

Tags:

jestjs

In Jest I need to test an expected value which can be either null or an object

{
  main: {
    prop1: 'abc',
    prop2: '123',
  }
}

but if it is an object, then I don't really care what main contains, I don't care about prop1 or prop2. I only need to assert that the object contains a key named main.

Jest reference mentions objectContaining, but it would still require that I specify at least one of the props, thus making my code unnecessarily verbose.

Is there any swift way to achieve an assertion that could be named objectContainingKey, like:

expect(something).toEqual(expect.objectContainingKey('main'))

like image 320
user776686 Avatar asked Dec 08 '22 14:12

user776686


1 Answers

The docs have something more suitable and more readable than the accepted answer:

expect(obj.main).toBeDefined();
like image 64
Lucio Mollinedo Avatar answered Jan 01 '23 13:01

Lucio Mollinedo