gpt4 book ai didi

javascript - 监视 bunyan 日志 - NodeJS

转载 作者:行者123 更新时间:2023-11-28 20:18:39 32 4
gpt4 key购买 nike

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

MyFile.js

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

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

测试

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);
});
});
})

最佳答案

是的,用Jest这很容易:

let spyLogWarn = jest.spyOn(require('bunyan').prototype, 'warn')
// ...
expect(spyLogWarn).toHaveBeenCalled()

关于javascript - 监视 bunyan 日志 - NodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51102246/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com