Custom Equality Testers
Learn how to write custom equality testers in Jasmine.
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
first
andsecond
. These are the two values that we want to compare. It must returntrue
,false
, orundefined
. -
In the
beforeEach
function of a test suite, extend Jasmine functionality using theaddCustomEqualityTester
method.
Get hands-on with 1200+ tech skills courses.