Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Custom error message of Matcher #272

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet

dsuket commented Aug 17, 2012

Usage:

var failMessage = 'obj.selected is not true. obj.id = ' + obj.id;
expect(obj.selected, failMessage).toBe(true)

By default, when obj.selected is false, "Expected false to be true" is printed. if you needs more information when test fail, that can show custom message.
I know get same effect to add message function to matcher object. but that is prolixity.

I like this idea, but may be it is more 'descriptive' to write like:

expect(obj.selected).toBe(true).because('object must be selected');

(Though, because will not be a matcher then)

Factually, it is a useful thing, I often have several conditions inside it('...', ...) blocks, and if one of them only tells Expected false to be truthy, I don't really get what happened.

Contributor

infews commented Sep 3, 2012

This pull request has no tests and modifies only the built jasmine.js file.

I'd like to treat this as a feature request. Let's move this to the mailing list and discuss further, hopefully leading us to the right implementation.

For example, one way to look at this is as a custom matcher that has its own message.

Closing for now. Looking forward to further discussion.

@infews infews closed this Sep 3, 2012

sylvinus commented Sep 5, 2012

There has already been a discussion about this and a because() patch:
https://groups.google.com/forum/?fromgroups=#!msg/jasmine-js/1dG11nfnsu8/9dvlPdM7u8IJ

+1 on implementing it :)

I'd love to see something like this added. Staring at "expected 1 to equal 2" is not terribly helpful.

i like this feature.

👍

EvHaus commented Oct 30, 2013

A .because() method seems like the most elegant solution. Currently we're relying on adding a bunch of console.logs around the expect calls so we can easily debug which one is failing.

aatishm commented Nov 8, 2013

I have come up with a similar approach but different implementation. See: aatishm/jasmine@73dce81

The issue with this commit: dsuket/jasmine@ce75e15 is that it loses Jasmine's valuable information "Expected toBe " type information; hence I had to come up with another solution.

It has been more than a year and we still don't have this feature. I would really like to see this feature as it kinda correlates to productivity.

I am in the process of contributing to Jasmine. (For some reason, I cannot install 'bundler' gem on my machine).

Can somebody please take a look at the above commit and tell what do they think about it?

aknuds1 commented Dec 25, 2013

Really hoping support for custom messages is added to Jasmine, as this is something I use all the time with Mocha (as the second argument to expect). In my case it's particularly useful because I am writing CoffeeScript and testing without source maps (can't get these to work to my satisfaction, but that's another discussion) so that the line number isn't very useful in finding the failed expectation.

gcbw commented Apr 19, 2016

work around for anyone that reaches here and despair. or to make an argument for this feature... cuts both ways :)

right out of our code base, enjoy:

// generator pattern just so we can have a meaningful error message
// on failures. curse karma-jasmine for not having a reason arg on expect()!
var goodUrls = [ '...' ]; // huge list.
for( url in goodUrls ){
    url = goodUrls[url];
    it('test good url: "'+url+'"', function (url) {
        return function(){
            expect( myClass.isValid(url) ).to.be.true;
        }
    }(url));
}
// now repeat the exact same block for `badUrls` but expecting .to.be.false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment