What is the simplest way to compare two arrays in QUnit
Asked Answered
A

1

11

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?

Accrescent answered 26/9, 2015 at 1:59 Comment(0)
B
14

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

Bipod answered 26/9, 2015 at 2:4 Comment(2)
Glad that worked! Please consider accepting the answer.Bipod
In a minute :) (stackoverflow's requirement for 5 minutes delay before acceptance becomes possible)Accrescent

© 2022 - 2024 — McMap. All rights reserved.