Sunday, January 31, 2021

how to use test object array contain element which match giving object?

 when we write a test case to verify that an object array contains specific object within JEST framework

we can use the following code to handle it .

let posts=[]

beforeAll(()=>{
    posts.push({title:'test'user:'dan'})
    posts.push({title:'hello world'user:'dan'})
})

test('test post exists',()=>{
    expect(posts).toEqual(
        expect.arrayContaining([
            expect.objectContaining({title: 'test'})
        ])
    )
})

No comments:

Post a Comment