如何在控制台中测试Winston logger消息的内容

问题描述 投票:0回答:1

我正在使用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的对象?任何帮助,将不胜感激!

我正在使用Winston作为我的应用程序的记录器,并且我正在尝试为记录器消息编写单元测试(使用Jest)。我正在特别尝试确保没有密码传递到我的日志中。由于...

javascript jest winston
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.