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

前端 未结 4 2048
春和景丽
春和景丽 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条回答
  • 2020-12-13 08:48

    Here's a pretty simple solution that uses SinonJS's test stubs to suppress all console.log/info/warn/error statements before running your tests.

    // my-method.js
    
    export function myMethod() {
        console.log(`I'm about to return true`)
        return true
    }
    

    // my-method.test.js
    
    import {describe, it, before} from 'mocha'
    import chai from 'chai'
    import sinon from 'sinon'
    import chalk from 'chalk'
    import {myMethod} from './my-method.js'
    
    const expect = chai.expect
    
    describe(chalk.underline('My Test Group'), () => {
    
        before(() => {
            sinon.stub(console, 'log')  // disable console.log
            sinon.stub(console, 'info')  // disable console.info
            sinon.stub(console, 'warn')  // disable console.warn
            sinon.stub(console, 'error')  // disable console.error
        })
    
        describe('myMethod', () => {
            it('should return true', () => {
                expect(myMethod()).to.be.true  // without printing to the console
            })
        })
    })
    

    // output
    
    My Test Group
      myMethod
        ✓ should return true
    
    0 讨论(0)
  • 2020-12-13 08:51

    Already answered but thought I would add that you can do this user winston.add()

    var logger = new (winston.Logger)({
        transports: [
            new (winston.transports.File)({filename: 'node.log'})
        ]
    });
    
    if (process.env.NODE_ENV === 'test') {
        logger.add(winston.transports.Console, {prettyPrint: true});
    }

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-13 09:02

    In your app.js:

    if (process.env.NODE_ENV !== 'test') {
      app.use(express.logger());
    }
    

    At the top of each of your mocha files:

    process.env.NODE_ENV = 'test';
    

    Update:

    We use this function in our import code:

    function logExceptOnTest(string) {
      if (process.env.NODE_ENV !== 'test') {
        console.log(string);
      }
    }
    

    Then, replace all your console.log('it worked') with logExceptOnTest('it worked'). The basic trick is to use environment variables as a global flag as to the level of logging you want.

    0 讨论(0)
提交回复
热议问题