toContain Vs. toContainEqual

The different usage between these two Jest matchers:

  • use toContain to verify the elements of an array or a string

    test('a specific blog is within returned blogs list', async () => {
    const response = await api.get('/api/blogs')
    const blogTitles = response.body.map(r => r.title)
    expect(blogTitles).toContain('CSS Tricks')
    })
  • use toContainEqual to verify the objects in an arrays

    describe('my beverage', () => {
    test('is delicious and not sour', () => {
    const myBeverage = {delicious: true, sour: false};
    expect(myBeverages()).toContainEqual(myBeverage);
    });
    });