Testing Views with jQuery matchers

Besides its HTML fixture module, the Jasmine jQuery extension comes with a set of custom matchers, which help in writing expectations with the DOM elements.

The biggest advantage of using these custom matchers, as demonstrated, is that they generate better error messages. So, although we can write all specs without using any of these matchers, it would get us much more useful information when an error happens if we used the matchers.

To better understand this advantage, we can revisit the example of the should expose a property with its DOM element spec. There, it uses the toExist matcher:

it("should expose a property with its DOM element", function() {
  expect(view.$element).toExist();
});

If this spec fails, we ...

Get Jasmine JavaScript Testing - Second Edition 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.