feat: Add the .fail([message]) interface

Fix #1116.
The `assert.fail` interface should accept being called with
only 1 arguments to fail with a custom message.
This commit is contained in:
Sylvain Leroux 2018-01-05 20:11:37 +01:00
parent f54f71c234
commit 0d1b5862fe
6 changed files with 96 additions and 12 deletions

View file

@ -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
@ -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

View file

@ -10,10 +10,18 @@ module.exports = function (chai, util) {
};
/**
* ### .fail([message])
* ### .fail(actual, expected, [message], [operator])
*
* Throw a failure.
*
* expect.fail();
* expect.fail("custom error message");
* expect.fail(1, 2);
* expect.fail(1, 2, "custom error message");
* expect.fail(1, 2, "custom error message", ">");
* expect.fail(1, 2, undefined, ">");
*
* @name fail
* @param {Mixed} actual
* @param {Mixed} expected
@ -24,6 +32,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

View file

@ -42,10 +42,19 @@ module.exports = function (chai, util) {
var should = {};
/**
* ### .fail([message])
* ### .fail(actual, expected, [message], [operator])
*
* Throw a failure.
*
* should.fail();
* should.fail("custom error message");
* should.fail(1, 2);
* should.fail(1, 2, "custom error message");
* should.fail(1, 2, "custom error message", ">");
* should.fail(1, 2, undefined, ">");
*
*
* @name fail
* @param {Mixed} actual
* @param {Mixed} expected
@ -56,6 +65,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

View file

@ -14,10 +14,24 @@ describe('assert', function () {
}, "expected foo to equal `bar`");
});
it('fail', function () {
chai.expect(function () {
assert.fail(0, 1, 'this has failed');
}).to.throw(chai.AssertionError, /this has failed/);
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('isTrue', function () {

View file

@ -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(){

View file

@ -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 () {