The behavior your noticed is Mocha's default behavior. Unless otherwise specified, when you run Mocha at the command line, you get the spec
reporter (whose class name is Spec
).
All reporters bundled with Mocha are based on the Base
reporter, which has this code:
runner.on('pass', function(test){
stats.passes = stats.passes || 0;
var medium = test.slow() / 2;
test.speed = test.duration > test.slow()
? 'slow'
: test.duration > medium
? 'medium'
: 'fast';
stats.passes++;
});
You can see there that tests that take more than the number of milliseconds deemed slow (--slow
option at the command line, default 75ms) are marked as slow
. Those that take more than half this time are marked as medium
and those that take less than this are marked fast
.
The code for the Spec
reporter does this:
runner.on('pass', function(test){
if ('fast' == test.speed) {
var fmt = indent()
+ color('checkmark', ' ' + Base.symbols.ok)
+ color('pass', ' %s ');
cursor.CR();
console.log(fmt, test.title);
} else {
var fmt = indent()
+ color('checkmark', ' ' + Base.symbols.ok)
+ color('pass', ' %s ')
+ color(test.speed, '(%dms)');
cursor.CR();
console.log(fmt, test.title, test.duration);
}
});
This code runs after the one in the Base
reporter (Base
initializes before Spec
). So by the time the handler in the previous code snippet runs, the test has been marked as slow
, medium
or fast
. As you can see, Mocha will report time only if the test is not fast.
You can expand the number of cases in which Mocha will report time by passing --slow 0
to the command line. (--slow -1
turns off time reporting completely.) However, you can still in theory get tests which take 0ms, and these tests will be deemed fast and not have any time reported.
If you want to force the time reporting for every test and use a reporter that works like the spec
reporter, I do not see a way to do it other than using your own custom reporter.