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 a nice error message, as shown in the following screenshot:
Now, we rewrite this spec without the custom matcher (still making the same validation):
it("should expose a property with its DOM element...