Writing Custom Matchers

Learn how to write and implement custom matchers.

Our applications are nuanced and will have nuanced testing needs. If Jest and other matcher libraries don’t suffice, we have the option to actually create our own matchers.

When to write a custom matcher

The tools that we already have will get us pretty far. Native matchers should be the first thing we reach for in tests. They are high quality, reliable, easily readable, and versatile.

Custom matchers should be reserved for specific logic that isn’t easily testable with these existing tools. Ultimately, a custom matcher will still execute in the same way. It will return true or false, and it will pass or fail based on whether or not something happened. ...