Jest: How to test for object keys and properties

fasenberg picture fasenberg · Dec 11, 2017 · Viewed 82.4k times · Source

I have a mapModule where I import components and export them:

import ComponentName from '../components/ComponentName';

export default {
  name: ComponentName,
};

How can I test it that mapModule has the correct exported keys, values and that they are not null or undefined?

Answer

user3605834 picture user3605834 · Jul 9, 2018

In version 23.3.0 of jest,

expect(string).toMatch(string) 

expects a string.

Use:

const expected = { name:'component name' }
const actual = { name: 'component name', type: 'form' }
expect(actual).toMatchObject(expected)

result is passing test