Besides its HTML fixture module, the Jasmine jQuery extension comes with a set of custom matchers, which help in writing expectations with elements in the DOM.
The biggest advantage of using these custom matchers, as demonstrated, is the better error messages they generate. So although you can write all your specs without using any of these matchers, doing so would get you much more useful information when an error happens.
To better understand this advantage, we can revisit the example of the spec should expose a property with its DOM element
. There, it uses the toExist
matcher:
it("should expose a property with its DOM element", function() {
expect(view.$el).toExist();
});
If this spec fails, we get a nice error message:
But if we rewrite this spec without the custom matcher (still making the same validation):
it("should expose a property with its DOM element", function() {
expect($(document).find(view.$el).length).toBeGreaterThan...