Assume following qunit test:
assert.equal({ foo: 'bar' }, { foo: 'bar' });
Those two object seem the same. So the assertion should pass. Right?
Turns out this is not how it works. And even the follow up cryptic message does not really help:
Diff suppressed as the expected and actual results have an equivalent serialization.
Well if we look into the documentation of qunit.assert we will find that:
The equal assertion uses the simple comparison operator (==) to compare the actual and expected arguments. When they are equal, the assertion passes; otherwise, it fails.
And the double equal symbol uses referential equality and since in the example above (and in most of my tests in general) I am not comparing the same object, but rather two objects of the same shape and values, it can never return true
.
The solution
The solution to this is really simple, just use deepEqual:
assert.deepEqual({ foo: 'bar' }, { foo: 'bar' });
Which gives me:
Photo by Andrea Piacquadio from Pexels
Top comments (0)