Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs: add count assertions wiki page to legacy docs
Signed-off-by: Kelechi Ebiri <ebiritg@gmail.com>
  • Loading branch information
TG199 committed Aug 20, 2025
commit c2119633e7175cb8eb7c40133f21d4845c136b64
48 changes: 48 additions & 0 deletions docs/api-tutorials/count-assertions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
While Mocha itself does not provide an assertion layer and cannot provide assertion counting, it's relatively easy to integrate this behavior using hooks. The following is a simplified version of an assertion counter:

```js

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;
actual = 0;
}

function check() {
if (!expected || 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);
})
})
```
3 changes: 3 additions & 0 deletions docs/api-tutorials/jsdoc.tutorials.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"count-assertions": {
"title": "Count assertions"
},
"custom-reporter": {
"title": "Create a Custom Reporter"
},
Expand Down
Loading