Custom matchers

Often a project will want to encapsulate custom matching code for use across multiple specs. Here is how to create a Jasmine-compatible custom matcher.

A custom matcher at its root is a comparison function that takes an actual value and expected value. This factory is passed to Jasmine, ideally in a call to beforeEach and will be in scope and available for all of the specs inside a given call to describe. Custom matchers are torn down between specs. The name of the factory will be the name of the matcher exposed on the return value of the call to expect.

This object has a custom matcher named "toBeGoofy".

const customMatchers = { 

Matcher Factories

Custom matcher factories are passed a matchersUtil parameter, which has a set of utility functions for matchers to use to perform tasks like determining whether two objects are equal (see: MatchersUtil for reference documentation). By using MatchersUtil where appropriate, custom matchers can work with custom equality testers and custom object formatters without any extra effort.

    toBeGoofy: function (matchersUtil) { 

The factory method should return an object with a compare function that will be called to check the expectation.

        return {