Skip to content

Commit 9b25d0c

Browse files
committed
Merge pull request #356 from Soviut/master
Added fail() method to Should and Expect interfaces
2 parents 5753ecb + ffd7c19 commit 9b25d0c

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

lib/chai/interface/expect.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,26 @@ module.exports = function (chai, util) {
88
chai.expect = function (val, message) {
99
return new chai.Assertion(val, message);
1010
};
11-
};
1211

12+
/**
13+
* ### .fail(actual, expected, [message], [operator])
14+
*
15+
* Throw a failure.
16+
*
17+
* @name fail
18+
* @param {Mixed} actual
19+
* @param {Mixed} expected
20+
* @param {String} message
21+
* @param {String} operator
22+
* @api public
23+
*/
24+
25+
chai.expect.fail = function (actual, expected, message, operator) {
26+
message = message || 'expect.fail()';
27+
throw new chai.AssertionError(message, {
28+
actual: actual
29+
, expected: expected
30+
, operator: operator
31+
}, chai.expect.fail);
32+
};
33+
};

lib/chai/interface/should.js

+22
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ module.exports = function (chai, util) {
4040

4141
var should = {};
4242

43+
/**
44+
* ### .fail(actual, expected, [message], [operator])
45+
*
46+
* Throw a failure.
47+
*
48+
* @name fail
49+
* @param {Mixed} actual
50+
* @param {Mixed} expected
51+
* @param {String} message
52+
* @param {String} operator
53+
* @api public
54+
*/
55+
56+
should.fail = function (actual, expected, message, operator) {
57+
message = message || 'should.fail()';
58+
throw new chai.AssertionError(message, {
59+
actual: actual
60+
, expected: expected
61+
, operator: operator
62+
}, should.fail);
63+
};
64+
4365
should.equal = function (val1, val2, msg) {
4466
new Assertion(val1, msg).to.equal(val2);
4567
};

test/expect.js

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ describe('expect', function () {
1010
expect('foo').to.equal('foo');
1111
});
1212

13+
it('fail', function () {
14+
err(function() {
15+
expect.fail(0, 1, 'this has failed');
16+
}, /this has failed/);
17+
});
18+
1319
it('true', function(){
1420
expect(true).to.be.true;
1521
expect(false).to.not.be.true;

test/should.js

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ describe('should', function() {
77
should.not.equal('foo', 'bar');
88
});
99

10+
it('fail', function () {
11+
err(function() {
12+
should.fail(0, 1, 'this has failed');
13+
}, 'this has failed');
14+
});
15+
1016
it('root exist', function () {
1117
var foo = 'foo'
1218
, bar = undefined;

0 commit comments

Comments
 (0)