8

Creating Matchers to Simplify Tests

This chapter introduces another method for simplifying tests: building custom matchers. Most of the time, it makes sense to stick to the built-in matchers. For instance, the powerful combination of the toEqual matcher with the expect.objectContaining and expect.arrayContaining functions make it easy to build expressive expectations.

But sometimes it makes sense to build a matcher that can scoop up a number of different checks into one single check. This not only shortens tests but can make them more readable, too.

In Chapter 5, Validating Form Data, each of the form validation rules was tested by a describe context with four tests, like this:

 describe('when the date of birth is in the wrong format')   it('does ...

Get Svelte with Test-Driven Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.