Merge pull request #1117 from s-leroux/assert/fail/only-one-argument

feat: Add the assert.fail([message]) interface
This commit is contained in:
Grant Snodgrass 2018-01-12 06:33:46 -05:00 committed by GitHub
commit d3908a6688
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 () {