mardi 3 novembre 2020

Jest expect object property value not to be object

I have an array of 'flat' objects, i.e. properties whose values are either a string, number, null etc.

let myArray = [
    {
        prop1: 'string'
        prop2: 123
    },
    {
        prop1: 'string'
        prop2: 123
    },
]

In this case I don't care what the actual property keys are and ideally I don't want to know what they are in advance, i just want to check the property values are objects.

The following would fail for example

let myArray = [
    {
        prop1: 'string'
        prop2: 123
    },
    {
        prop1: 'string'
        prop2: {
            prop3: true
        }
    },
]

How can I write this in a jest test?

expect(myArray).toEqual(
    expect.arrayContaining([
        expect.toBeObject()
        expect.objectValues.not.toBeObject    // What do I actually put here
    ])
);

I am using jest extended.

Aucun commentaire:

Enregistrer un commentaire