Using JEST I want to test if an array of objects is a subset of another array.
I want to test the following:
JavaScript
x
5
1
const users = [{id: 1, name: 'Hugo'}, {id: 2, name: 'Francesco'}, {id: 3, name: 'Carlo'}];
2
const subset = [{id: 1, name: 'Hugo'}, {id: 2, name: 'Francesco'}];
3
4
expect(users).toContain(subset)
5
I’ve tried the following:
JavaScript
1
10
10
1
describe('test 1', () => {
2
it('test 1', () => {
3
expect(users).toEqual(
4
expect.arrayContaining([
5
expect.objectContaining(subset)
6
])
7
)
8
});
9
});
10
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:
JavaScript
1
4
1
expect(users).toEqual(
2
expect.arrayContaining(subset)
3
)
4