Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Mocha report time for every test?

Using mocha to do node.js unit tests I get e.g. this output:

Suite One:
  call Home Page
    √ should return correct result (65ms)
  call Login Page
    √ should return empty load
  do Login
    √ should return login details (53ms)
  call Dashboard
    √ should return empty load

  6 passing (192ms)

Why for two test cases I get the test time (65/53 ms), but not for the other two cases? Is there a certain option? I only found --slow but nothing more.

Add: if the tests are slow, I get times for all test cases:

Suite One:
 call Home Page 
  √ should return correct result (1155ms)
 call Login Page
  √ should return empty load (359ms)
 do Login
  √ should return login details (703ms)
 call Dashboard
  √ should return empty load (347ms)

it seems, if the test cases are very fast, then I get no time .. ?

like image 502
Rainer Avatar asked Aug 12 '15 20:08

Rainer


People also ask

How does a Mocha test work?

Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.

Does Mocha run tests in order?

Mocha will run the tests in the order the describe calls execute.

How do you skip the Mocha test?

Find out how you can ignore some tests in Mocha You can use the skip() method (on describe() or it() ) to ignore some tests: In case of describe , it will ignore all tests in the describe block (which includes all nested describe and it functions); In case of it , it will ignore the individual test.

Does Mocha run tests in parallel?

Mocha does not run individual tests in parallel. That means if you hand Mocha a single, lonely test file, it will spawn a single worker process, and that worker process will run the file. If you only have one test file, you'll be penalized for using parallel mode.


1 Answers

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.

like image 128
Louis Avatar answered Jan 03 '23 04:01

Louis