Comparing two arrays
Here is my code: https://codepen.io/lukakuwu/pen/zYyPYRm?editors=0010
I have an array with prices and a function to add the tax. I made a second array with the expected results and used jest to test. I know theres something wrong with how im calling the results arrays values but I dont know how to fix it. The tests should pass through the array dishes twice and compare all of the items in the array results with the price. Maybe theres a better way to reformat it but Ive tried everything I can think of
5 Replies
you can't compare arrays
when you compare an array to another, you're effectively checking if it is the same instance or not
since they are 2 different instances, they will always be different
https://jestjs.io/docs/expect#expectarraycontainingarray <-- you can try this instead
Expect · Jest
When you're writing tests, you often need to check that values meet certain conditions. expect gives you access to a number of "matchers" that let you validate different things.
if you want to make sure that the array is EXACTLY the same, aparently you can sort both arrays
https://stackoverflow.com/a/40172358 <-- according to this
Stack Overflow
Is there an Array equality match function that ignores element posi...
I get that .toEqual() checks equality of all fields for plain objects:
expect(
{"key1":"pink wool","key2":"diorite"}
).toEqual(
{"key2":&q...
you can also use
JSON.stringify()
and compare the result (which will be picky about the order of the elements)