2
votes

I'm looking a way to add custom messages on succeeded tests with protractor and jasmine. There're many ways to customize messages on failure, like jasmine-custom-message node package or simply:

expect(column.get(0)).toEqual("7", "This is not something I've expected");

But I haven't found a way to add custom messages on success. I'm using protractor-jasmine2-html-reporter to generate my reports, but successful tests only display a "passed" or "0 failure" messages and I'd like to be more explicit about what am I testing and why the tests passed.

1
Unfortunately, there is nothing built into jasmine for that: github.com/jasmine/jasmine/issues/1127..alecxe

1 Answers

-1
votes

You want to write a custom matcher for Jasmine. You want to include them in the protractor.conf.js file so they are accessible over the entire project

https://jasmine.github.io/2.0/custom_matcher.html

Here is an example of comparing numbers with the message printing off if there is an error. The error if you run this will be Expected 7 to be 8 (Expected Error). Note, this is including it in the spec using the beforeEach.

var customMatchers = {
  toBeEqualWithMessage: function(util, customEqualityTesters) {
    return {
      compare: function(actual, expected, message) {
        var result = {};
        result.pass = util.equals(actual, expected, customEqualityTesters);
        if (!result.pass) {
          result.message = "Expected " + actual + " to be " + expected + " (" + message + ")";
        }
        return result;
      }
    };
  }
};

describe('Custom Matcher', function(){
  beforeEach(function() { jasmine.addMatchers(customMatchers);});

  it('should use a matcher', function(){
    expect(7).toBeEqualWithMessage(7,"No error because they match");
    expect(7).not.toBeEqualWithMessage(8,"No error because of the 'not'");
    expect(7).toBeEqualWithMessage(8,"Expected Error");
  });
});

FYI: The doc has a result.message for a passing case, but I don't know where that is used, maybe a verbose print out.