Skip to content

Commit

Permalink
feat: Add the .fail([message]) interface
Browse files Browse the repository at this point in the history
Fix chaijs#1116.
The `assert.fail` interface should accept being called with
only 1 arguments to fail with a custom message.
  • Loading branch information
s-leroux committed Jan 11, 2018
1 parent f54f71c commit 833324d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 9 deletions.
15 changes: 15 additions & 0 deletions lib/chai/interface/assert.js
Expand Up @@ -41,10 +41,18 @@ module.exports = function (chai, util) {
};

/**
* ### .fail([message])
* ### .fail(actual, expected, [message], [operator])
*
* Throw a failure. Node.js `assert` module-compatible.
*
* assert.fail();
* assert.fail("custom error message");
* assert.fail(1, 2);
* assert.fail(1, 2, "custom error message");
* assert.fail(1, 2, "custom error message", ">");
* assert.fail(1, 2, undefined, ">");
*
* @name fail
* @param {Mixed} actual
* @param {Mixed} expected
Expand All @@ -55,6 +63,13 @@ module.exports = function (chai, util) {
*/

assert.fail = function (actual, expected, message, operator) {
if (arguments.length < 2) {
// Comply with Node's fail([message]) interface

message = actual;
actual = undefined;
}

message = message || 'assert.fail()';
throw new chai.AssertionError(message, {
actual: actual
Expand Down
6 changes: 6 additions & 0 deletions lib/chai/interface/expect.js
Expand Up @@ -10,6 +10,7 @@ module.exports = function (chai, util) {
};

/**
* ### .fail([message])
* ### .fail(actual, expected, [message], [operator])
*
* Throw a failure.
Expand All @@ -24,6 +25,11 @@ module.exports = function (chai, util) {
*/

chai.expect.fail = function (actual, expected, message, operator) {
if (arguments.length < 2) {
message = actual;
actual = undefined;
}

message = message || 'expect.fail()';
throw new chai.AssertionError(message, {
actual: actual
Expand Down
6 changes: 6 additions & 0 deletions lib/chai/interface/should.js
Expand Up @@ -42,6 +42,7 @@ module.exports = function (chai, util) {
var should = {};

/**
* ### .fail([message])
* ### .fail(actual, expected, [message], [operator])
*
* Throw a failure.
Expand All @@ -56,6 +57,11 @@ module.exports = function (chai, util) {
*/

should.fail = function (actual, expected, message, operator) {
if (arguments.length < 2) {
message = actual;
actual = undefined;
}

message = message || 'should.fail()';
throw new chai.AssertionError(message, {
actual: actual
Expand Down
28 changes: 27 additions & 1 deletion test/assert.js
Expand Up @@ -14,12 +14,38 @@ describe('assert', function () {
}, "expected foo to equal `bar`");
});

describe("fail", function() {
it('should accept a message as the 3rd argument', function () {
err(function() {
assert.fail(0, 1, 'this has failed');
}, /this has failed/);
});

it('should accept a message as the only argument', function () {
err(function() {
assert.fail('this has failed');
}, /this has failed/);
});

it('should produce a default message when called without any arguments', function () {
err(function() {
assert.fail();
}, /assert\.fail()/);
});
});

it('fail', function () {
chai.expect(function () {
assert.fail(0, 1, 'this has failed');
assert.fail('this has failed');
}).to.throw(chai.AssertionError, /this has failed/);
});

it('fail', function () {
chai.expect(function () {
assert.fail();
}).to.throw(chai.AssertionError, /assert\.fail/);
});

it('isTrue', function () {
assert.isTrue(true);

Expand Down
22 changes: 18 additions & 4 deletions test/expect.js
Expand Up @@ -236,10 +236,24 @@ describe('expect', function () {
, 'of', 'same', 'but', 'does' ].forEach(test);
});

it('fail', function () {
err(function() {
expect.fail(0, 1, 'this has failed');
}, /this has failed/);
describe("fail", function() {
it('should accept a message as the 3rd argument', function () {
err(function() {
expect.fail(0, 1, 'this has failed');
}, /this has failed/);
});

it('should accept a message as the only argument', function () {
err(function() {
expect.fail('this has failed');
}, /this has failed/);
});

it('should produce a default message when called without any arguments', function () {
err(function() {
expect.fail();
}, /expect\.fail()/);
});
});

it('true', function(){
Expand Down
22 changes: 18 additions & 4 deletions test/should.js
Expand Up @@ -233,10 +233,24 @@ describe('should', function() {
, 'of', 'same', 'but', 'does' ].forEach(test);
});

it('fail', function () {
err(function() {
should.fail(0, 1, 'this has failed');
}, 'this has failed');
describe("fail", function() {
it('should accept a message as the 3rd argument', function () {
err(function() {
should.fail(0, 1, 'this has failed');
}, /this has failed/);
});

it('should accept a message as the only argument', function () {
err(function() {
should.fail('this has failed');
}, /this has failed/);
});

it('should produce a default message when called without any arguments', function () {
err(function() {
should.fail();
}, /should\.fail()/);
});
});

it('root exist', function () {
Expand Down

0 comments on commit 833324d

Please sign in to comment.