Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest way to compare two arrays in QUnit

I'm writing JavaScript unit tests (with the QUnit library). I need to verify that my array contains expected (and only) elements.

var array = getArrayFunction(a, b);
equal(["one", "two", "three"], array, "Test is failing even if 'array' contains needed elements");

What would be the easiest way to do this?

like image 982
Vova Avatar asked Sep 26 '15 01:09

Vova


People also ask

How do I compare two arrays to each other?

Programmers who wish to compare the contents of two arrays must use the static two-argument Arrays. equals() method. This method considers two arrays equivalent if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equivalent, according to Object. equals() .

Can you compare arrays in Arduino?

However for future reference there is a simple way to compare two arrays OF THE SAME TYPE AND SIZE and that is to use the memcmp() function which compares two blocks of memory and returns an integer greater than 0 if the first block is greater than the second, returns 0 if the two blocks are the same, and an integer ...

How do I compare two arrays in C sharp?

Compare Arrays in C# Using == (Equality Operator) This method is going to receive the two arrays we want to compare as parameters. After checking if they are equal using == , it is going to return a bool indicating the result of this operation.

How do I compare two arrays of arrays?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.


1 Answers

You should use deepEqual() in lieu of equal(). That will compare array elements and object properties, not just use the == comparison operator, which evaluates to false for objects that don't share the same constructor.

Docs here: https://api.qunitjs.com/deepEqual/

More information about JavaScript equality comparison: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

like image 185
Bungle Avatar answered Oct 22 '22 05:10

Bungle