Custom Equality Testers
Explore how to implement custom equality testers in Jasmine to improve readability and flexibility of your Angular unit tests. You will learn to override default equality checks, handle special cases like dates and strings, and write cleaner tests that better suit your application's data comparisons.
We'll cover the following...
Jasmine lets us override its equality tester for a test suite. This means that we could provide our own definition of the toEqual function, which would be used instead of the default definition. This technique can be useful in making a test suite more readable. It also gives us more flexibility, particularly for comparing objects.
To define a custom equality tester, we must do two things:
-
Define a function that takes arguments
firstandsecond. These are the two values that we want to compare. It must returntrue,false, orundefined. -
In the
beforeEachfunction of a test suite, extend Jasmine functionality using theaddCustomEqualityTestermethod.