How to run jasmine tests without overriding messages from passed expects?

261 views
Skip to first unread message

Julio Herce

unread,
Nov 11, 2015, 4:00:45 PM11/11/15
to Jasmine
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](https://github.com/avrelian/jasmine2-custom-message) and [protractor-jasmine2-html-reporter](https://github.com/Kenzitron/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?

Gregg Van Hove

unread,
Nov 13, 2015, 5:29:40 PM11/13/15
to jasmi...@googlegroups.com
Please see my reply to the issue you had already made here: https://github.com/jasmine/jasmine/issues/971

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at http://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

Julio Herce

unread,
Dec 2, 2015, 7:45:51 AM12/2/15
to Jasmine
Hello,

First of all, thank you for your reply. 

I understand that you don't want to add this feature in jasmine. But is there any way to override the default jasmine's buildExpectationResult in (for example) protractor config file?

Gregg Van Hove

unread,
Dec 2, 2015, 12:43:28 PM12/2/15
to jasmi...@googlegroups.com
It is probably possible to override construction of error messages, but since all the code is internal to jasmine and not intended for external use, it could change at any time and break your code/suite.

-Gregg
Reply all
Reply to author
Forward
0 new messages