【问题标题】:Can I hide failure details in mocha output?我可以在 mocha 输出中隐藏失败详细信息吗?
【发布时间】:2018-06-25 22:46:40
【问题描述】:

有时在运行一组 mocha 测试时,我并不关心失败的细节;我只想要一个通过或失败的测试列表。我试过几个记者,但他们似乎都输出了失败的细节。我喜欢默认的规范报告器结构,但我找不到如何隐藏细节。

这是一个说明性示例。对于这些测试:

const assert = require('assert')
describe('test test', function() {
  it('should pass', function() {

  })
  it('should fail', function() {
    assert(false)
  })
})

输出如下:

  test test
    ✓ should pass
    1) should fail


  1 passing (9ms)
  1 failing

  1) test test
       should fail:

      AssertionError [ERR_ASSERTION]: false == true
      + expected - actual

      -false
      +true

      at Context.<anonymous> (test-solution.js:69:5)

但我想要的只是这个:

  test test
    ✓ should pass
    1) should fail

  1 passing (9ms)
  1 failing

我是否遗漏了一些明显的东西,或者这些细节是我无法压制的?

【问题讨论】:

    标签: javascript mocha.js


    【解决方案1】:

    我也想隐藏这一点,对我来说,大多数默认记者似乎也不是那么好。每条无用的行都会花费我们时间。在我看来,自定义输出应该非常简单。

    构建自己的自定义报告器是您问题的正确答案。但是 - 因为这让我很长时间 - 这里有一个非常简短和简单的选择:禁用记者并记录一些事件日志。

    const Mocha = require('mocha');
    let file = './devtest.js';
    let passCount = 0;
    let errors = [];
    
    // start with disabled reporter
    const mocha = new Mocha({ reporter: function () {} });
    mocha.addFile(file);
    console.log('\n===== start mocha file ' + file);
    
    mocha.run()
       .on('pass', function (test) {
          passCount++;
          logSuccess(test.title);
       })
       .on('fail', function (test, err) {
          errors.push({test, err});
          logError(test.title);
       })
       .on('end', function () {
          console.log();
          console.log('   -------------------------');
          logSuccess(passCount + ' tests passed');
          logError(errors.length + ' tests failed');
          // do something here - like:
          // callback(errors)
       });
    
    function logSuccess (str) {
       console.log('\u001b[32m  ✓ \u001b[0m\u001b[90m' + str + '\u001b[0m');
    }
    function logError (str) {
       console.log('\u001b[31m  ✖ ' + str + '\u001b[0m');
    }
    
    

    当然,与标准报告器相比,它有一些功能较少,但扩展非常简单 - 您拥有所有错误和数据。所以速度非常快。

    也许其他任何人都可以为此发布一个非常简单的工作示例自定义报告器 - 对我来说,自定义报告器破坏了我的控制台输出,我对更多调试不感兴趣。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多