简体   繁体   中英

Can I hide failure details in mocha output?

Sometimes when running a set of mocha tests I don't care about failure details; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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