Let’s suppose that I have the following code:
const id = '1' const arrayOfIds = ['1', '2', '3']
And I want to check if id is in arrayOfIds
Something like this
expect(id).toBeIn(arrayOfIds)
How can I do this with Jest?
Advertisement
Answer
Use .toContain when you want to check that an item is in an array. For testing the items in the array, this uses ===, a strict equality check.
test('is id in arrayOfIds', () => {
const id = '1';
const arrayOfIds = ['1', '2', '3'];
expect(arrayOfIds).toContain(id);
});