Skip to content

Commit

Permalink
Added assertion counting (fixes chaijs#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Nov 23, 2013
1 parent f82f43f commit e103da6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/chai/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module.exports = function (_chai, util) {
var AssertionError = _chai.AssertionError
, flag = util.flag;

var count = 0;

/*!
* Module export.
*/
Expand Down Expand Up @@ -81,6 +83,14 @@ module.exports = function (_chai, util) {
util.overwriteMethod(this.prototype, name, fn);
};

Assertion.getCount = function() {
return count;
};

Assertion.resetCount = function() {
count = 0;
};

/*!
* ### .assert(expression, message, negateMessage, expected, actual)
*
Expand All @@ -96,7 +106,10 @@ module.exports = function (_chai, util) {
*/

Assertion.prototype.assert = function (expr, msg, negateMsg, expected, _actual, showDiff) {

var ok = util.test(this, arguments);
count++;

if (true !== showDiff) showDiff = false;
if (true !== Assertion.showDiff) showDiff = false;

Expand Down
21 changes: 21 additions & 0 deletions lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ module.exports = function (chai, util) {
);
};

/**
* ### .asserted()
*
* Asserts that assertions have been made.
*
* @name asserted
* @api public
*/

assert.asserted = function () {
var act = Assertion.getCount();
var test = new Assertion(null);

test.assert(
act > 0
, 'expected one or more assertions'
, '[ negation message unavailable ]'
);

};

/**
* ### .fail(actual, expected, [message], [operator])
*
Expand Down
11 changes: 11 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ describe('assert', function () {
}, "expected 'test' to be true");
});

it('asserted', function() {
chai.Assertion.resetCount();
assert.isTrue(true);
assert.asserted();

chai.Assertion.resetCount();
err(function() {
assert.asserted();
}, "expected one or more assertions");
});

it('ok', function () {
assert.ok(true);
assert.ok(1);
Expand Down

0 comments on commit e103da6

Please sign in to comment.