Search code examples
javascriptarraysexistselementqunit

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?


Solution

  • 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