user4445419
user4445419

Reputation:

Mocha and should assertion and not working as expected

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});

Upvotes: 1

Views: 189

Answers (3)

den bardadym
den bardadym

Reputation: 2815

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

Upvotes: 0

bastijn
bastijn

Reputation: 5953

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.

Upvotes: 0

danillouz
danillouz

Reputation: 6371

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.

Upvotes: 1

Related Questions