简体   繁体   中英

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?

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

在此输入图像描述

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

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 ')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM