• Skip to primary navigation
  • Skip to main content

cultivate

  • Home
  • Products
  • Blog
  • Newsletter
  • Contact

Test Anti-pattern: Not using the best available matcher

Here’s another common test anti-pattern that makes working with tests harder than it needs to be.

๐Ÿ›‘ Test Anti-pattern ๐Ÿ›‘

Not using the best available matcher


Matchers are little helper functions that let you verify test conditions more elegantly.

Most testing frameworks like Jest come with a predefined set of matchers (a.k.a. assertions). For instance:

expect(sum).toBe(42)
expect(skills).toContain('TDD');
expect(temperature).toBeLessThan(100)

Not using the best available matcher can significantly decrease the readability โ€” and waste precious development time:

โŒ avoid:

expect(circumference).toBe(Math.round(PI * 2 * 2 * 10000) / 10000)

โœ… prefer:

expect(circumference).toBeCloseTo(PI * 2 * 2, 4)

Besides improved readability and time savings, there’s another benefit. Using the best possible matcher will also improve the error message presented to you when a test fails:

โŒ avoid:

expect(items.length).toBe(3)

โœ… prefer:

expect(items).toHaveLength(3)

The difference between the two examples above is the output in case of a bug.

If items is unexpectedly undefined, the example to avoid will result in the following error message:

TypeError: Cannot read properties of undefined (reading 'length')

This error message could be more helpful, and a developer might lose precious development time while trying to figure out what’s happening.

With .toHaveLength(..), however, we would get an improved error message:

expect(received).toHaveLength(expected)

Matcher error: received value must have a length property whose value must be a number

Received has value: undefined

By the way, you don’t have to limit yourself to the predefined matchers, either. Jest Extended is an excellent library of additional matchers like .toBePositive() or toEndWith(..) that shouldn’t be missing in any project.

Here are again the three benefits of using the correct matcher:

  • test code is easier to read
  • less test code to write
  • error messages are more helpful

Each of the above points represents a reduction of development time and thus costs.

So, always use the best matcher available!

See you around,
-David

Sign up for the cultivate newsletter to get our best advice straight to your inbox

Weโ€™ll keep you posted with a few emails per month. You can, of course, opt out at any time.

Connect

  • Email
  • LinkedIn
  • Phone
  • YouTube

Legal

Imprint
Privacy Policy

Copyright © 2023 ยท cultivate GmbH

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. ACCEPTCookie settingsRead More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT