Jest: How to test for object keys and properties

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?

Answers


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


you can use one of those:

toEqual and toMatch are template matchers for objects:

let Obj = {name: 'component name', id: 2};
expect(oneObj).toEqual({name: 'component name'}) // false, should be exactly equal all Obj keys and values  
expect(oneObj).toMatchObject({name: 'component name'}) // true

or easly use toHaveProperty :

let Obj = {name: 'component name'};
expect(oneObj).toHaveProperty('name') // true
expect(oneObj).toHaveProperty('name', 'component name') // true

Need Your Help

Calling virtual method in base class constructor

java c# c++ constructor

I know that calling a virtual method from a base class constructor can be dangerous since the child class might not be in a valid state. (at least in C#)

How to Capitalize first letter only using CSS in each case

css word capitalization

I want to Capitalize first letter only and other should be small using CSS