【发布时间】:2020-04-19 15:48:29
【问题描述】:
我正在使用 Winston 作为我的应用程序的记录器,并且我正在尝试为记录器消息编写单元测试(使用 Jest)。我特别想确保没有密码传递给我的日志。
由于记录器输出到stdout,我找不到访问我的winston.error 函数输出并在日志对象上运行我的断言的方法。
我尝试了几种不同的方法来访问记录器的结果,并在谷歌上搜索了解决方案,但没有取得多大成功。这是我的最后一次迭代:
我的 logger.js(内容参数在传递给记录器之前已经过清理):
export const error = (content) => {
return winston.error(content);
};
还有我的 logger.spec.js:
const exampleLogs = { "email":"email", "password":"password" };
describe('Passwords are filtered out of Winston logger messages', () => {
it('Should filter out password data from "error" level errors', () => {
logger.error(exampleLogs);
const result = jest.fn();
expect(result).not.toHaveProperty('password');
});
});
即使密码密钥就在那里,测试也通过了。当我console.log(result) 我得到一个模拟构造函数对象:
{ [Function: mockConstructor]
_isMockFunction: true,
getMockImplementation: [Function],
mock: [Getter/Setter],
mockClear: [Function],
mockReset: [Function],
mockRestore: [Function],
mockReturnValueOnce: [Function],
mockResolvedValueOnce: [Function],
mockRejectedValueOnce: [Function],
mockReturnValue: [Function],
mockResolvedValue: [Function],
mockRejectedValue: [Function],
mockImplementationOnce: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
mockName: [Function],
getMockName: [Function] }
但我希望看到我的exampleLogs 对象...如何访问记录器发送到stdout 的对象?任何帮助将不胜感激!
【问题讨论】:
标签: javascript jestjs winston