Why doesn't Mocha report time for every test?
Asked Answered
D

2

24

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 .. ?

Danilodanio answered 12/8, 2015 at 20:51 Comment(0)
C
49

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.

Crudity answered 17/8, 2015 at 16:48 Comment(0)
G
0

I appreciate the existing answer but after reading it, I didn't understand how to make my tests not show up as red using JS/TS (I didn't want to use a custom test command).

To make your tests not show up as red (slow), use .slow()

From the [Mocha documentation]:(https://mochajs.org/#test-duration)

FAST: Tests that run within half of the “slow” threshold will show the duration in green (if at all).

My tests usually take 16 seconds, but this is normal as they involve connecting to a local Solana validator and running a bunch of transactions.

.slow() sets the 'slow threshold'. If I set .slow() to 60 * SECONDS, my tests will be green as long as they are lower than 30 * SECONDS

it("does something", async () => {
  await doThing();
}).slow(60 * SECONDS);

Hopefully obviously:

const SECONDS = 1000;

Before

red warnings

After

no timing warnings

Gromyko answered 26/7 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.