I am currently working on creating a test for appendfilesync function. When using a logger, I noticed that one line of code is not covered in my tests. Below is the code snippet I am referring to (please note that I am using tslog for logging purposes):
export function logToTransport(logObject: ILogObject) {
appendFileSync('monopoly_deal.log', JSON.stringify(logObject) + '\n');
}
I have attempted to mock 'fs' in order to prevent actual file writing during testing, but this method does not allow me to properly test the writing functionality. Here is the beginning of my test setup code:
const log: Logger = new Logger({ name: 'card_types_test' });
log.attachTransport(
{
silly: CardTypes.logToTransport,
debug: CardTypes.logToTransport,
trace: CardTypes.logToTransport,
info: CardTypes.logToTransport,
warn: CardTypes.logToTransport,
error: CardTypes.logToTransport,
fatal: CardTypes.logToTransport,
},
'info',
);
// To intercept the call without executing it, I believe I need to set up jest.spyon. However, I am unsure about the correct approach. Possibly something like const spy = jest.fn(CardTypes.logToTransport);
log.info('test'); // Writes to the log file
expect(spy).toequal({object with my data}); // The specific structure of the object has been omitted for brevity.
Any advice on how to simulate file writing in my tests would be highly valuable, as well as any other suggestions or feedback (as I am still relatively new to programming).