Skip to content

Commit

Permalink
Merge pull request #356 from Soviut/master
Browse files Browse the repository at this point in the history
Added fail() method to Should and Expect interfaces
  • Loading branch information
keithamus committed Feb 10, 2015
2 parents 5753ecb + ffd7c19 commit 9b25d0c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/chai/interface/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,26 @@ module.exports = function (chai, util) {
chai.expect = function (val, message) {
return new chai.Assertion(val, message);
};
};

/**
* ### .fail(actual, expected, [message], [operator])
*
* Throw a failure.
*
* @name fail
* @param {Mixed} actual
* @param {Mixed} expected
* @param {String} message
* @param {String} operator
* @api public
*/

chai.expect.fail = function (actual, expected, message, operator) {
message = message || 'expect.fail()';
throw new chai.AssertionError(message, {
actual: actual
, expected: expected
, operator: operator
}, chai.expect.fail);
};
};
22 changes: 22 additions & 0 deletions lib/chai/interface/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ module.exports = function (chai, util) {

var should = {};

/**
* ### .fail(actual, expected, [message], [operator])
*
* Throw a failure.
*
* @name fail
* @param {Mixed} actual
* @param {Mixed} expected
* @param {String} message
* @param {String} operator
* @api public
*/

should.fail = function (actual, expected, message, operator) {
message = message || 'should.fail()';
throw new chai.AssertionError(message, {
actual: actual
, expected: expected
, operator: operator
}, should.fail);
};

should.equal = function (val1, val2, msg) {
new Assertion(val1, msg).to.equal(val2);
};
Expand Down
6 changes: 6 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ describe('expect', function () {
expect('foo').to.equal('foo');
});

it('fail', function () {
err(function() {
expect.fail(0, 1, 'this has failed');
}, /this has failed/);
});

it('true', function(){
expect(true).to.be.true;
expect(false).to.not.be.true;
Expand Down
6 changes: 6 additions & 0 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ describe('should', function() {
should.not.equal('foo', 'bar');
});

it('fail', function () {
err(function() {
should.fail(0, 1, 'this has failed');
}, 'this has failed');
});

it('root exist', function () {
var foo = 'foo'
, bar = undefined;
Expand Down

0 comments on commit 9b25d0c

Please sign in to comment.