jasmine

Custom Matchers

Adding Custom Matchers

Custom matchers can be added in jasmine using the syntax:

jasmine.addMatchers([
    toMatch: function () {
    return {
        compare: function (actual, expected) {
            return {
                pass: actual===expected,
                message: "Expected actual to match expected
            }
        }
    }
}
]);

This matcher can now be called with:

expected(actual).toMatch(expected);

Negative Matchers

Custom matchers will have their pass value negated when using ‘not’. Custom matchers can have a negative compare attribute to explicitly handle cases where their negation is desired:

toMatch: function () {
        return {
            compare: function (actual, expected) {
                return {
                    pass: actual===expected,
                    message: "Expected actual to match expected"
                }
            },
            negativeCompare: function(actual, expected){
                return {
                    pass: actual!==expected,
                    message: "Expected actual not to match expected"
                }
            }
        }
    }

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow