1

I use Mocha and should as my test frameworks to node js modules. Until now Its works OK ,now I need to assert two object to equal. and I got error (test fail) while the object are the same (I use webStorm 10) and the Diff window show the two object and they Identical (I see also message in the diff window Contents are identical ...)

what It can be and there is a way to overcome this issue?

I try with both which fails

should(inObjBefore).be.exactly({env: outObjAfter});

 inObjBefore.should.be.exactly({ env: outObjAfter});

3 Answers3

1

exactly does an exact comparison using strict equality, i.e. ===. In javascript Objects are stored by reference and not by value. Therefore when comparing two Objects, they will only equal each other when they are of the same reference:

var a = {
  x: 10
};

a === a // true
a === { x: 10 } // false

So either you need to compare to the same Object or you can use deepEqual.

danillouz
  • 6,121
  • 3
  • 17
  • 27
  • I try with .should. inObjBefore.should.be.deepEqual({ env: outObjAfter}); and I got error? This is what you mean? –  Aug 20 '15 at 21:40
  • btw Both object looks exactly the same {env:obj} and {env:obj2} –  Aug 20 '15 at 21:42
  • The obj vs obj2 is exactly why it fails. It only succeeds if obj is equal (obj instead of obj2). Seems you want to compare the attributes / properties not the references / instances. Use equals. – bastijn Aug 20 '15 at 21:46
0

I can't test this right now but it might be that should.be.exactly is checking for the exact same object instance while you have two instances and you are interested in knowing if their properties are equal.

I.e.

A = object.with.name.is.Joe
B = otherObject.with.name.is.Joe
a.should.equal(b) = true
À.should.be.exactly(b) = false

Sorry om nu phone, cant verify this.

bastijn
  • 5,841
  • 5
  • 27
  • 43
0

You need to use deep object comparison. Use .eql or .deepEqual(alias to .eql). .exactly is the same as .equal and doing reference comparison with ===.

den bardadym
  • 2,747
  • 3
  • 25
  • 27