1

I am writing test cases for winston custom format, but here the printf from winston does the custom format. So how can I test the customFormat, using jest.

Here the customFormat which I am exporting is a variable, how can I test this one, in jest coverage report this line is not covered. So how can test this one

//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")
 })
})
Lin Du
  • 88,126
  • 95
  • 281
  • 483
Learner
  • 8,379
  • 7
  • 44
  • 82
  • How about using `winston.format.printf`? The error make sense to me since `printf` is not defined in the code you show – Rashomon Jan 31 '19 at 15:12
  • So how can I change the snippet – Learner Jan 31 '19 at 15:14
  • Is `printf` defined anywhere? Even without tests `winston.js` should throw an error since you are exporting an undefined variable `printf` – Rashomon Jan 31 '19 at 15:33
  • i am importing the printf it is defined it is a function from winston library – Learner Jan 31 '19 at 17:09
  • @Rashomon any way to test this one – Learner Feb 02 '19 at 07:39
  • I recommend not using a snippet if its no able to run. Makes the question confusing. Also, `printf` returns a `Printf` instance, not a string. So dont expect to return anything like `"info -- winston.js -- dummy text"`. Instead I would call `logger` directly. Then you would need to read your `option.file` and check if its logged correctly – Rashomon Feb 02 '19 at 10:20
  • but the problem is i have written the test cases for all others but when i run the coverage report the the line customFormat is not covered how can i test that one – Learner Feb 02 '19 at 10:22
  • @Rashomon i have moved out the customFormat from the logger function , check this one .And i am trying to test it https://stackoverflow.com/questions/54491134/testing-a-function-inside-another-function-using-jest – Learner Feb 02 '19 at 10:23

0 Answers0