Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spying on bunyan log - NodeJS

Is there any way in which I can spy on the bunyan log to ensure I print out what I expect?

MyFile.js

const bunyan = require('bunyan');
const log = bunyan.createLogger({name: 'FailureAuditService'});

class someClass {
   someFunct() {
     if(x) {
        log.warn('something happened');
     }
   }
}

Test

const service = require(../MyFile);

describe('test something', () => {
    it('Will test the bunyan log', res => {
       let consoleLog = sinon.spy(log, 'createLogger');
       let x = true;

       service.someClass(x).then(res => {
          let expected = 'something happened';
          consoleLog.should.equal(expected);
       });
    });
})
like image 382
physicsboy Avatar asked Jan 28 '23 20:01

physicsboy


1 Answers

Yes, with Jest it's quite easy:

let spyLogWarn = jest.spyOn(require('bunyan').prototype, 'warn')
// ...
expect(spyLogWarn).toHaveBeenCalled()
like image 126
jonas Avatar answered Jan 30 '23 09:01

jonas