Chai: Is there a way to show a custom error with should syntax?

Created on 9 Oct 2013  路  12Comments  路  Source: chaijs/chai

81 explained a way to show a custom error message with the expect syntax. Can the same be done with "should"... if so how exactly would that look?

Most helpful comment

Such a small functionality could be incorporated without much trouble on the core.
Creating a plugin for this may be an overkill and that plugin would be too empty, IMO.

All 12 comments

The withMessage plugin example also works with should interface.

x.should.withMessage('topic [x]').be.true;

@logicalparadox where is this plugin? http://chaijs.com/plugins/ doesn't have it or any text matching withMessage

@jcollum I'm not aware of such a plugin existing; I believe #81 was merely demonstrating how it might be done.

It's pretty straightforward to implement, though. The following standalone example works:

let chai = require("chai");

chai.should();

chai.use(function (_chai, _) {
  _chai.Assertion.addMethod('withMessage', function (msg) {
    _.flag(this, 'message', msg);
  });
});

it("experiment", () => {
  let experiment = false;

  experiment.should.withMessage("Error. Human is Dead, mismatch").be.true;
});

Excellent, thanks.

@meeber I feel like it would be good to have this implemented into the core, it seems to be a common use case.
What do you guys think? Should we?

If you're not using property assertions, you can already provide a custom message. For example:

expect(true).to.equal(true, 'value is true');
true.should.equal(true, 'value is true');

Property assertions, by their nature, cannot have arguments, so this _wont_ work:

expect(true).to.be.true // no extra message

I think something like .withMessage would be a reasonable addition since there's currently no way to customize the message for should property assertions.

With expect at least you can customize the message like this:

expect(true, "ahhh spiders!").to.be.false;

We had an issue before about this, I can't seem to find it... we eventually determined that there are enough ways to set the message and the issue was closed. I wonder if this is something we should jump to adding given all of that?

In that case, maybe a small plugin would be better. And/or a note in the documentation on how to customize a message with should property assertions.

Such a small functionality could be incorporated without much trouble on the core.
Creating a plugin for this may be an overkill and that plugin would be too empty, IMO.

I don't have a strong opinion on this. I'd support just about any approach :D

What I came looking for was something along the lines of what @keithamus suggested, ended up with:

error.called.should.equal(true, 'error was not called')

That works for me with no additions or plugins. I just had a hard time figuring out _when_ I could pass in that second arg to describe the failure.

The true/false failures get really cryptic when you have a stack of two or three of them.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

meeber picture meeber  路  3Comments

qbolec picture qbolec  路  5Comments

ghost picture ghost  路  4Comments

meeber picture meeber  路  4Comments

corybill picture corybill  路  4Comments