O'Reilly logo

Jasmine JavaScript Testing by Paulo Ragonha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Understanding matchers

By now, you've already seen plenty of usage examples for matchers, and probably can feel how they work.

You have seen how to use the toBe , the toEqual , and toBeTruthy matchers. These are a few of the built-in matchers available in Jasmine, but we can extend Jasmine by writing matchers of our own.

So, to really understand how Jasmine matchers work, we need to create one ourselves.

Custom matchers

Consider this expectation from the previous section:

expect(investment.isGood()).toBeTruthy();

Although it works, it is not very expressive. Imagine if we could rewrite it instead, as:

expect(investment).toBeAGoodInvestment();

This makes a much better relation to the acceptance criterion:

"should be a good investment"→expect investment ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required