forked from mochajs/mocha
-
Notifications
You must be signed in to change notification settings - Fork 67
Assertion counting
TJ Holowaychuk edited this page Jul 2, 2013
·
1 revision
While Mocha itself does not provide an assertion layer and cannot provide assertion counting, it's relatively easy to integrate this behaviour using hooks. The following is a simplified version of an assertion counter:
var expected = 0;
var actual = 0;
function assert(expr, msg) {
if (!expr) throw new Error(msg || 'assertion failed');
actual++;
}
function expect(n) {
expected = n;
}
function reset() {
expected = 0;
}
function check() {
if (expected == actual) return;
var err = new Error('expected ' + expected + ' assertions, got ' + actual);
this.currentTest.emit('error', err);
}
beforeEach(reset);
afterEach(check);
describe('something', function(){
it('should work', function(done){
expect(2);
setTimeout(function(){
assert('wahoo')
}, 50);
setTimeout(function(){
assert('hey')
}, 50);
setTimeout(function(){
done();
}, 100);
})
})