Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
1
var assert = require('assert');
2
3
var mustCallChecks = [];
4
5
function runCallChecks() {
6
var failed_count = 0;
7
for (var i=0 ; i< mustCallChecks.length; ++i) {
8
var context = mustCallChecks[i];
9
if (context.actual === context.expected) {
10
continue;
11
}
12
13
failed_count++;
14
console.log('Mismatched %s function calls. Expected %d, actual %d.',
15
context.name,
16
context.expected,
17
context.actual);
18
console.log(context.stack.split('\n').slice(2).join('\n'));
19
}
20
21
assert(failed_count === 0);
22
}
23
24
after(runCallChecks);
25
26
exports.mustCall = function(fn, expected) {
27
if (typeof expected !== 'number') expected = 1;
28
29
var context = {
30
expected: expected,
31
actual: 0,
32
stack: (new Error).stack,
33
name: fn.name || '<anonymous>'
34
};
35
36
mustCallChecks.push(context);
37
38
return function() {
39
context.actual++;
40
return fn.apply(this, arguments);
41
};
42
};
43
44