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 ...

Get Jasmine JavaScript Testing now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.