目前我正在使用 jest 测试 NodeJS 应用程序。特别是系统看起来像这样:
当我运行测试时,我喜欢订购日志。特别是在第一次测试然后深入日志时。我知道,那个 Jest 可以选择
--silent
.有趣的是,此选项不适用于 winston 。 winston 的源代码看起来像是在尝试直接写入 stdout/err。有人知道,在使用
--silent
运行测试时如何让 winston 保持沉默吗?选项
最佳答案
您可以使用 setupFilesAfterEnv
jest option ,通过将其指向一个将模拟 console
的文件.例如
将此添加到 jest.config.js
:
{
...
setupFilesAfterEnv: ["./jest.setup.js"],
...
}
然后创建一个文件
jest.setup.js
global.beforeAll(() => {
console.error = jest.fn().mockImplementation(() => {});
console.info = jest.fn().mockImplementation(() => {});
console.log = jest.fn().mockImplementation(() => {});
});
The source code of winston looks like it is trying to directly write to stdout/err.
请注意,由于您将 winston 配置为打印到 stdout\stderr,因此上面的以下建议应该可以解决问题。当然,您可能希望为
--silent
添加额外的检查。旗帜否则,你可以有条件到silence winston通过使用
silent
winston 运输的属性(property)silent: If true, all logs are suppressed
关于node.js - 如何关闭来自 winston 的日志对于Jest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62127610/