Custom Equality Testers

Learn how to write custom equality testers in Jasmine.

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:

  1. Define a function that takes arguments first and second. These are the two values that we want to compare. It must return true, false, or undefined.

  2. In the beforeEach function of a test suite, extend Jasmine functionality using the addCustomEqualityTester method.

Get hands-on with 1200+ tech skills courses.