Testing a JavaScript function that returns an array of numbers involves checking if the returned array contains the same elements as the expected output:
expect(myArray).toEqual(expectedArray);
The comparison works well with arrays containing only integers, but when floats are present, it fails due to floating-point precision errors. Using toBeCloseTo
on arrays doesn't seem to solve the issue.
Currently, I am using a loop for member-wise verification:
for (var i = 0; i < myArray.length; i++) {
expect(myArray[i]).toBeCloseTo(expectedArray[i]);
}
However, is there a more concise way of achieving this? When the test fails, it generates an excessive number of error messages in the output.