Reputation: 799
I'm writing my logs in JSON format and here is part of my logger.
const enrichCorrelationId = format((info) => {
const correlationId = correlator.getId();
if (correlationId) {
info.correlationId = correlator.getId();
return info;
}
return info;
});
const customFormat = format.combine(
format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
enrichCorrelationId(),
format.json({ space: 2 }),
format.colorize({ all: true }),
);
here is how I'm mocking in test.
jest.mock("winston", () => {
const mFormat = {
combine: jest.fn().mockReturnThis(),
timestamp: jest.fn().mockReturnThis(),
printf: jest.fn().mockReturnThis(),
colorize: jest.fn(),
};
const mTransports = {
Console: jest.fn(),
};
const mAddColors = jest.fn();
return {
createLogger: jest.fn(),
format: mFormat,
transports: mTransports,
addColors: mAddColors,
};
});
Obviously, it is going to fail at the const enrichCorrelationId = format((info) => {
saying format is not a function, as the mock is just an object.
If I change the mock to be a function, then the test is failing at the combine
timestamp
colors
json
since these are expecting format to be an object.
how can we mock format to be both an object and also function, so I can test the logic that is happening in the enrichCorrelationId
function?
Upvotes: 0
Views: 118