Using JEST I want to test if an array of objects is a subset of another array.
I want to test the following:
const users = [{id: 1, name: 'Hugo'}, {id: 2, name: 'Francesco'}, {id: 3, name: 'Carlo'}]; const subset = [{id: 1, name: 'Hugo'}, {id: 2, name: 'Francesco'}]; expect(users).toContain(subset)
I’ve tried the following:
describe('test 1', () => { it('test 1', () => { expect(users).toEqual( expect.arrayContaining([ expect.objectContaining(subset) ]) ) }); });
But this is not correct since objectContaining doesn’t accept array as param … it works only if subset is a single object.
Advertisement
Answer
I’ve never tried this myself, but wouldn’t it work to just say:
expect(users).toEqual( expect.arrayContaining(subset) )