Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow messages to be functions #268

Merged
merged 1 commit into from
Sep 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/chai/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ module.exports = function (_chai, util) {
*
* @name assert
* @param {Philosophical} expression to be tested
* @param {String} message to display if fails
* @param {String} negatedMessage to display if negated expression fails
* @param {String or Function} message or function that returns message to display if fails
* @param {String or Function} negatedMessage or function that returns negatedMessage to display if negated expression fails
* @param {Mixed} expected value (remember to check for negation)
* @param {Mixed} actual (optional) will default to `this.obj`
* @api private
Expand Down
1 change: 1 addition & 0 deletions lib/chai/utils/getMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = function (obj, args) {
, msg = negate ? args[2] : args[1]
, flagMsg = flag(obj, 'message');

if(typeof msg === "function") msg = msg();
msg = msg || '';
msg = msg
.replace(/#{this}/g, objDisplay(val))
Expand Down
4 changes: 4 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ describe('assert', function () {
err(function () {
assert(foo == 'baz', "expected foo to equal `bar`");
}, "expected foo to equal `bar`");

err(function () {
assert(foo == 'baz', function() { return "expected foo to equal `bar`"; });
}, "expected foo to equal `bar`");
});

it('fail', function () {
Expand Down
7 changes: 7 additions & 0 deletions test/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ describe('utilities', function () {
var obj = {};
_.flag(obj, 'message', 'foo');
expect(_.getMessage(obj, [])).to.contain('foo');

var obj = {};
msg = function() { return "expected a to eql b"; }
negateMsg = function() { return "expected a not to eql b"; }
expect(_.getMessage(obj, [null, msg, negateMsg])).to.equal("expected a to eql b");
_.flag(obj, 'negate', true);
expect(_.getMessage(obj, [null, msg, negateMsg])).to.equal("expected a not to eql b");
});
});

Expand Down