监视 bunyan 日志 - NodeJS

phy*_*boy 4 javascript testing node.js sinon bunyan

有什么方法可以监视 bunyan 日志以确保打印出我期望的内容?

我的文件

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

class someClass {
   someFunct() {
     if(x) {
        log.warn('something happened');
     }
   }
}
Run Code Online (Sandbox Code Playgroud)

测试

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);
       });
    });
})
Run Code Online (Sandbox Code Playgroud)

jon*_*nas 7

是的,使用Jest很容易:

let spyLogWarn = jest.spyOn(require('bunyan').prototype, 'warn')
// ...
expect(spyLogWarn).toHaveBeenCalled()
Run Code Online (Sandbox Code Playgroud)