O'Reilly logo

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

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

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