Situation is as follows: I want to test a function of mine, which returns an array of objects. The objects have no enumerable properties, but are taken from a common cache/store.
In my testcases I now want to test, whether the contents of the returned array are correct. So I assumed, that deepEqual()
would be the function to use, but the tests actually return this (reduced the testcase a little):
QUnit.test( "array of objects", function( assert ) {
// create two objects
var o1 = new Obj( 1 ),
o2 = new Obj( 2 );
// compare objects themselves
assert.notEqual( o1, o2, 'different objects' ); // success
// compare identical arrays
assert.deepEqual( [ o1 ], [ o1 ], 'identical arrays - deepEqual' ); // success
// compare different arrays
assert.notDeepEqual( [ o1 ], [ o2 ], 'different arrays - deepEqual' ); // fail
});
// some sample object
function Obj(){}
(I also tested propEqual()
to see, if that one works here.)
So QUnit recognizes the two objects as different (see first test), but fails to recognize the difference as soon as I use the array.
So I played around a little and as soon as I have an enumerable property on my object, in which both instances differ, QUnit will recognize it: Changed Fiddle
function Obj( val ){ this._val = val; }
Interesting enough, if the enumerable property is set to the same value by using this:
var o1 = new Obj( 1 ),
o2 = new Obj( 1 );
I see the same behavior as with no property (Fiddle).
So, with the above examples in mind,
How do I force QUnit to compare two objects by identity in order to check the contents of an array?
as per discussion with @Ilya here a rephrase of the actual question:
How can I use deepEqual()
just for one level - meaning, all elements of the given array should be compared by object identity?
PS: I know the naive solution would be to compare element by element in the array, but I think there should be a faster way.