Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is there a way to print a custom error message when a Jasmine expect() fails?

As an example, for end to end testing I have an array of web pages and I use one test to go to each URL and assert an element exists on each page. I know I can put every expect() into a separate test, but I'd rather iterate through the array and log the page URL on failure.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
292 views
Welcome To Ask or Share your Answers For Others

1 Answer

UPDATE

I see people still are finding this. Later information from the Jasmine team is that there is an undocumented feature on the expect - you can include a custom failure message and it just works:

expect( fields[i].element.exists() ).toEqual(true, field[i].name + ' is expected to exist');

Which is exactly what I was originally looking for.

Original answer follows:

I've been looking for exactly this today, and put a comment here: https://github.com/adobe/brackets/issues/2752

The syntax that has been discussed is an extension to Jasmine to permit a because to be added - so you'd be able to write:

expect( fields[i].element.exists() ).toEqual(true).because( field[i].name + 'is expected to exist');

That is still being discussed after a few years, and may not come to fruition. Another way that I've found to do this is to create a custom matcher. In general I think I'd discourage a custom matcher without being sure you're covering all the bases with it, but in this case we're really checking a true/false value, so the matcher isn't too scary.

We can create the custom matcher with a beforeEach:

beforeEach(function() {
  var matchers = {
    toEqualBecause: function( value, message ) {
      this.message = function() {
        return "Expected '" + this.actual + "' to equal '" + value + "' because " + message;  
      };

      return this.actual == value;  
    }
 };

  this.addMatchers(matchers);
});

We can then use this matcher to put a message with our failures as such:

expect( field[i].element.exists() ).toEqualBecause( true, field[i].name );

Which will give a failure output including the field name as such:

Expected 'false' to equal 'true' because account_name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...