protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Is there a possibility in jasmine not to override messages from passed expects?

Open lynchblue opened this issue 11 years ago • 1 comments

I am using in my project protractor for my e2e tests with jasmine2 as framework. I am using as well the libraries jasmine2-custom-message and protractor-jasmine2-html-reporter.

I have realised that the message from passed expects would be overridden with the word: Passed.

After a little research I realised that the file that is actually overridden this message is the following: node_modules/protractor/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js

Specifically the function:

getJasmineRequireObj().buildExpectationResult = function () {
    function buildExpectationResult(options) {
        var messageFormatter = options.messageFormatter || function () {
                },
            stackFormatter = options.stackFormatter || function () {
                };

        var result = {
            matcherName: options.matcherName,
            message: message(),
            stack: stack(),
            passed: options.passed
        };

        if (!result.passed) {
            result.expected = options.expected;
            result.actual = options.actual;
        }

        return result;

        function message() {
            if (options.passed) {
                return 'Passed.';
            } else if (options.message) {
                return options.message;
            } else if (options.error) {
                return messageFormatter(options.error);
            }
            return '';
        }

        function stack() {
            if (options.passed) {
                return '';
            }

            var error = options.error;
            if (!error) {
                try {
                    throw new Error(message());
                } catch (e) {
                    error = e;
                }
            }
            return stackFormatter(error);
        }
    }

    return buildExpectationResult;
};

I wonder if there is a way to override this function (buildExpectationResult), for example in protractor config file. I have already tried that by adding in protractor config file the following.

onPrepare: function () {
    jasmine.buildExpectationResult = function () {
        function buildExpectationResult(options) {
            var messageFormatter = options.messageFormatter || function () {
                    },
                stackFormatter = options.stackFormatter || function () {
                    };

            return {
                matcherName: options.matcherName,
                expected: options.expected,
                actual: options.actual,
                message: message(),
                stack: stack(),
                passed: options.passed
            };

            function message() {
                if (options.passed && !options.message) {
                    return 'Passed.';
                } else if (options.message) {
                    return options.message;
                } else if (options.error) {
                    return messageFormatter(options.error);
                }
                return "";
            }

            function stack() {
                if (options.passed) {
                    return "";
                }

                var error = options.error;
                if (!error) {
                    try {
                        throw new Error(message());
                    } catch (e) {
                        error = e;
                    }
                }
                return stackFormatter(error);
            }
        }

        return buildExpectationResult;
    };
}

But that is not working.

Is there any other way of overriding jasmine functions?

lynchblue avatar Nov 11 '15 15:11 lynchblue

Please direct general support questions like this one to an appropriate support channel, see https://github.com/angular/protractor/blob/master/CONTRIBUTING.md#questions

This looks like a question for Jasmine, consider using the jasmine mailing list at https://groups.google.com/forum/#!forum/jasmine-js

juliemr avatar Nov 11 '15 16:11 juliemr