Search code examples
javascripttestingjasmineprotractorend-to-end

Add a custom matcher in Jasmine 1.3 - shows undefined


I'm using protractor with jasmine 1.3, tried adding a custom matcher to my spec using the example here

    beforeEach(function () {
        utils.log("beforeEach");

        this.addMatchers({
            toBeGoofy: function (expected) {

                if (expected === undefined) {
                    expected = '';
                }
                var pass = this.actual.hyuk === "gawrsh" + expected;
                if (pass) {
                    this.message = "Expected " + this.actual + " not to be quite so goofy";
                } else {
                    this.message = "Expected " + this.actual + " to be goofy, but it was not very goofy";
                }

                return pass;
            },
        });
    });

note that I didn't change anything from their example. after that, i try using it inside an "it" like that:

expect({ "hyuk": "j" }).toBeGoofy();

and i get an error:

TypeError: undefined is not a function

on the line that the matcher was used on.. any help?


Solution

  • The problem was the matcher definition apparently. instead of:

    if (pass) {
        this.message = "Expected " + this.actual + " not to be quite so goofy";
        } else {
        this.message = "Expected " + this.actual + " to be goofy, but it was not very goofy";
    }
    

    this message should be an array of 2 messages, first for pass, second for not.matcher so it would be something like this:

    this.message = function() {
        return [
            "Expected " + this.actual.hyuk + " to be gawrsh",
            "Expected " + this.actual.hyuk + " not to be gawrsh"
        ];
    };