How to suppress application logging messages from a node.js application when running unit tests?

前端 未结 4 2053
春和景丽
春和景丽 2020-12-13 08:37

While unit-testing my node.js application (which is basically a REST backend) using mocha and supertest, I need only the test-specific message on the screen, but the stdout

4条回答
  •  -上瘾入骨i
    2020-12-13 08:53

    Another way would be using mocha-suppress-logs to hide logs generated by successuful tests but still keep the ones generated by failed tests to ease debugging.

    Install:

    npm install --save-dev mocha-suppress-logs
    

    Then use it like this:

    const suppressLogs = require('mocha-suppress-logs');
     
    describe('Something', () => {
      suppressLogs();
     
      it('should do something', () => {
        // test code
      });
    });
    

    You can also do so globally for the entire test suite.

    Here's a link to the module itself:

    https://www.npmjs.com/package/mocha-suppress-logs

提交回复
热议问题