首页 > 解决方案 > 使用 jest 测试 winston 的格式

问题描述

我正在为 winston 自定义格式编写测试用例,但这里printf来自 winston 的自定义格式。那么我该如何测试customFormat, 使用 jest.

这里customFormat我导出的是一个变量,我该如何测试这个,在开玩笑的覆盖率报告中,这条线没有被覆盖。那么如何测试这个

//winston.js
const customFormat = printf((log) => {
  return `${log.level} -- ${log.label} -- ${log.message} `;
});


const logger = () => createLogger({
  format: combine(
    label({
      label: "test label"
    }),
    format.timestamp(),
    customFormat,
  ),
  transports: [
    new transports.File(options.file),
  ],
  exitOnError: false, // do not exit on handled exceptions
});


module.exports = {
  logger,
  customFormat,
  printf
};


// winston.test.js

describe('winton test cases', () => {
 test("check custom format is returning", () => {
   console.log(customFormt) // undeifned
   expect(customFormat).toEqual("info -- winston.js -- dummy text")
 })
})

标签: javascriptjestjs

解决方案


推荐阅读