user2167582
user2167582

Reputation: 6368

how does jasmine compare complex objects and arrays

In jasmine, how does comparison helpers such as expect(a).toEqual() work in the source code? Does it check by ducktyping?

Upvotes: 1

Views: 102

Answers (2)

sfletche
sfletche

Reputation: 49704

The short answer

toEqual compares all the properties of both objects and returns true if the corresponding values are equal.

The long answer

You can follow the trail of toEqual in the source code and eventually land here in the eq method which gives the gritty details...(and I quote the source: 'lovingly adapted from isEqual in Underscore')

Upvotes: 1

alecxe
alecxe

Reputation: 473763

Whenever you are not sure, look into the source code:

enter image description here

(taken from Learn to Read the Source, Luke).

I won't quote the actual implementation (it's quite big), but here is the underlying util function performing the check.

FYI, it is actually adapted from _.isEqual() and uses stacks to perform deep object and array comparisons.

Note that you can extend the jasmine equality check by adding your custom equality testers.

Upvotes: 2

Related Questions