简体   繁体   English

我可以在 mocha 输出中隐藏失败细节吗?

[英]Can I hide failure details in mocha output?

Sometimes when running a set of mocha tests I don't care about failure details;有时在运行一组 mocha 测试时,我并不关心失败的细节; I only want a list of tests with pass or fail.我只想要一个通过或失败的测试列表。 I've tried several reporters, but they all seem to output details for failures.我试过几个记者,但他们似乎都输出失败的详细信息。 I like the default spec reporter structure, but I can't find how to hide the details.我喜欢默认的规范报告结构,但我找不到如何隐藏细节。

Here's an illustrative example.这是一个说明性示例。 For these tests:对于这些测试:

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

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

Which gives output like this:这给出了这样的输出:

  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)

but what I want is just this:但我想要的只是这个:

  test test
    ✓ should pass
    1) should fail

  1 passing (9ms)
  1 failing

Am I missing something obvious, or are these details just not something I can suppress?我是否遗漏了一些明显的东西,或者这些细节不是我可以压制的?

I wanted to hide this too and for me it seems also that most default reporters are not so nice.我也想隐藏这一点,对我来说,大多数默认记者似乎也不是那么好。 Each line that is useless will cost us time.每条无用的线路都会花费我们时间。 In my eyes it should be very simple to customize the output.在我看来,自定义输出应该非常简单。

Building a own custom reporter is the correct answer to your question.构建自己的自定义记者是您问题的正确答案。 However - as this took me to long - here a very short and easy alternative: Disable the reporter and do some logs on the events.然而 - 因为这花了我很长时间 - 这里有一个非常简短和简单的替代方案:禁用报告器并在事件上做一些日志。

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');
}

在此处输入图片说明

Of course this has some features less compared to the standard reporter, but extending is pretty simple - you have all the errors and data.当然,与标准报告器相比,它的一些功能较少,但扩展非常简单 - 您拥有所有错误和数据。 So it is very fast.所以它非常快。

Perhaps anybody else can post a very simple working example a custom reporter for that - for me the custom reporter broke my console output and I was not interested in more debugging.也许其他任何人都可以发布一个非常简单的工作示例,一个自定义报告器 - 对我来说,自定义报告器破坏了我的控制台输出,我对更多调试不感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM