After each test where winston logger is printing into a logfile some msg, i need to delete the file. However, nothing happens at fs.unlinkSync(logFile); where logFile is the path to the logfile. It is not throwing any error nor deleting the file. Winston output gets printed properly in the logfile. The path to logfile provided to fs.unlinkSync is correct as well.
import { getLogger, createWinstonTransports } from '../logger';
import fs from 'fs';
import path from 'path';
import winston from 'winston';
describe('getLogger', () => {
const logFile = path.join(__dirname,'logfile.log');
const loggers = winston.createLogger({
transports: createWinstonTransports(logFile),
exitOnError: false,
});
const logger = getLogger('Jest test', loggers);
afterEach(() => {
// Delete the log file after each test
if (fs.existsSync(logFile)) {
try {
fs.unlinkSync(logFile);
} catch (err) {
console.error('Error deleting the log file: ', err );
}
} else {
console.log('The file does not exist')
}
}, 5000)
it('should return a logger', () => {
logger.info('This is an info message');
});
});```