是否可以去除摩卡报告的毫秒速度?

Is it possible to remove the millisecond speed reported by Mocha?

在Mocha/Chaijavascript测试框架中是否可以去除Mocha报告的毫秒速度?

我想要一种更简单的方法来比较不同测试的输出,目前每条测试线都显示差异,因为速度 (ms) 从 运行 运行.

test1.js
    description here
      ✔ should do the things (211ms)

Mocha 使用 spec 作为其默认值 reporter.The spec reporter 继承自 Base reporter。它使用 Base.epilogue() 方法用于 EVENT_RUN_END 事件。

该方法会打印毫秒数,源码:

Base.consoleLog(fmt, stats.passes || 0, milliseconds(stats.duration));

你可以 create a custom reporter 摩卡。

./reporters/tidy.js:

const Mocha = require('mocha');
const { EVENT_RUN_END, EVENT_TEST_FAIL, EVENT_TEST_PASS } = Mocha.Runner.constants;

class Tidy {
  constructor(runner) {
    const stats = runner.stats;
    runner
      .on(EVENT_TEST_PASS, (test) => {
        console.log(`pass: ${test.fullTitle()}`);
      })
      .on(EVENT_TEST_FAIL, (test, err) => {
        console.log(`fail: ${test.fullTitle()} - error: ${err.message}`);
      })
      .once(EVENT_RUN_END, () => {
        console.log(`end: ${stats.passes}/${stats.passes + stats.failures} ok`);
      });
  }
}

module.exports = Tidy;

使用fullTitle() → {string}

Return the full title generated by recursively concatenating the parent's full title.

它会给你 pass: <test suite title> <test case title> 个字符串。

示例测试:

const { expect } = require('chai');

describe('description here', () => {
  it('should pass', () => {
    expect(1 + 1).to.be.equal(2);
  });
  it('should fail', () => {
    expect(1 + 1).to.be.equal(3);
  });
});

输出:

⚡  npx mocha --reporter ./reporters/tidy.js /Users/dulin/workspace/github.com/mrdulin/expressjs-research/reporters/index.test.js 
pass: description here should pass
fail: description here should fail - error: expected 2 to equal 3
end: 1/2 ok

虽然 更复杂,但我发现了一种更便宜的方法来消除速度,方法是将 --slow 选项增加到一个非常大的数字,因为速度只出现在慢速测试中.