2

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(){}

Fiddle for the testcase

(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.

Sirko
  • 72,589
  • 19
  • 149
  • 183

1 Answers1

0

How do I force QUnit to compare two objects by identity in order to check the contents of an array?

If you want to test that t1 = [ o1, o2 ] and t2 = [ o1, o2 ] are different objects, equal does exactly that.

Conversely, if you want to test that t1 = [ o1, o2 ] and t2 = [ o1, o2 ] are equal, you would have to create a custom assertion specifically for the array-containing-the-same-objects case.

deepEqual evaluates (recursively) the properties of the objects, what else could it do ?

Ilya
  • 5,377
  • 2
  • 18
  • 33
  • I want to test that `[ o1 ]` is not equal to `[ o2 ]`, but `t1 = [ o1 ]` and `t2 = [ o1 ] ` are. – Sirko Jan 06 '16 at 16:55
  • `assert.equal( [o1], [o1], '' )` fails for the same reason that `assert.equal( o1, o2, '' )` fails: same properties, but different objects. Seems consistent to me. – Ilya Jan 06 '16 at 17:04
  • ok, fair point, removed that sentence from the question, but the overall problem remains the same. Inside the array I want just to compare by object identities, call it a "`deepEqual`-one-level-deep" variant. – Sirko Jan 06 '16 at 17:06
  • I'lm afraid you would have to create your own assert for that (`arrayEqual` ?) – Ilya Jan 06 '16 at 17:08