0

Disclaimer: TypeScript (and more generally JavaScript) object equality functions have been discussed en-masse here (e.g., https://stackoverflow.com/a/63496057) and everywhere else (e.g., https://bobbyhadz.com/blog/typescript-object-comparison). I am not looking for any code snippets (there are more than enough in How to determine equality for two JavaScript objects?). There are various libraries (underscore, lodash, fast-deep-equal, fast-equal, nano-equal, etc.) for object equality checking, but it's hard to understand the differences between them.

What I miss in most questions and answers about TypeScript object equality is unit testing.
Because the testing framework decides what should be considered "equal".
I use Jasmine for unit tests, thus the toEqual(…) method governs object equality.

For example, JSON.stringify(…) is not consistent with Jasmine's toEqual(…) because JSON.stringify(…) is sensitive to property order:

JSON.stringify({a:1, b:2}) != JSON.stringify({b:2, a:1});

expect({a:1, b:2}).toEqual({b:2, a:1});

For full consistency, an equality check would have to yield the same result as Jasmine's toEqual(…) for every conceivable input.

I am aware that Jasmine supports custom equality testers, but these should only be used for special cases.

Question: Are any of the aforementioned equality libraries fully consistent with Jasmine's toEqual(…)?
If none of them are, which are the edge cases that yield different results than Jasmine?

[Edit: Rephrased the question to avoid asking for subjective advice on off-site libraries; added two more equality libraries to the list; clarified what I mean by full consistency with Jasmine.]

MyKey_
  • 837
  • 1
  • 7
  • 22
  • Asking for off-site libraries is specifically off-topic on Stack Overflow. See the [help/on-topic]. – Heretic Monkey Aug 04 '22 at 23:20
  • @HereticMonkey, thanks for the reminder. Would the question remain on-topic if rephrased to "Are any of the aforementioned libraries fully consistent with Jasmine's 'toEqual(…)' method?" – MyKey_ Aug 04 '22 at 23:41
  • Well, that wouldn't be off-topic, but would probably garner downvotes, since you can test them as easily (if not more easily) than anyone else can. – Heretic Monkey Aug 04 '22 at 23:46
  • Have you looked at Jasmine's `toEqual` implementation? Can you share its source code? – Bergi Aug 05 '22 at 02:38
  • @HereticMonkey, I have rephrased the question as discussed. Could you please explain your concern about possible downvotes? I am neither a TypeScript expert nor affiliated with any of those projects. I asked the question because I assume there are slight functional differences between equality libraries (e.g., whether functions are ignored, cycle detection, '=='-vs-'===' strictness of equality for primitive fields, or other subtle considerations as discussed in https://stackoverflow.com/a/201471). – MyKey_ Aug 05 '22 at 06:15
  • @Bergi, The corresponding Jasmine source code https://github.com/jasmine/jasmine/blob/e022e6199c77482b1e2c9b683590f791909b6216/src/core/matchers/matchersUtil.js#L168 states that it was "lovingly adopted from Underscore". This is a good hint but I since the code was "adopted" I am not sure whether it's functionally equivalent. – MyKey_ Aug 06 '22 at 21:01
  • 1
    @MyKey_ Ah I guess the major distinction from Underscore is that Jasmine builds a diff to be printed, for showing *what* the difference is instead of just returning a boolean. So yeah, it's hard to tell what exactly it does. – Bergi Aug 06 '22 at 22:58

0 Answers0