Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To test multiple values that are returned using Jest, you can use the expect.arrayContaining matcher. This matcher allows you to check that an expected array contains all the elements that are returned in the actual array.

For example, if you have a function that returns an array of numbers [1,2,3], you could test it like this:

test('returns an array of numbers', () => {
  expect(myFunction()).toEqual(expect.arrayContaining([1,2,3]));
});

This will ensure that the array returned by myFunction contains all the expected numbers, but it may contain additional values as well. If you want to ensure that the array only contains the expected values, you can add the expect.arrayContaining matcher to an expect.not.arrayContaining matcher:

test('returns only expected numbers', () => {
  expect(myFunction()).toEqual(expect.not.arrayContaining([expect.anything()]));
  expect(myFunction()).toEqual(expect.arrayContaining([1,2,3]));
});

This will ensure that the array returned by myFunction does not contain any other values besides those specified in the expected array.