You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I mock away my file system, the read operation seems to happen before Winston starts writing the log.
With the latest version of mock-fs, they've changed on which event loop they operate, they switched from process.nextTick() to setImmediate(), which, as far as I understand the issue, should be fine.
The reason I think this might be a winston issue, and not a mock-fs issue, is because I tried replicating the issue I am facing with many fs-based functions, which all seem to work the intended way, unlike winston.
Here is a minimal example of what I've tried with fs vs what I am trying to get working with Winston.
Mocking and directly using fs functions:
it('tests mock-fs in isolation',async()=>{constrootNodeModules=path.resolve(__dirname,'../../../../node_modules');mock({'test.log': 'content',[rootNodeModules]: mock.load(rootNodeModules)});awaitfs.promises.writeFile('test.log','new-content');constcontent=awaitfs.promises.readFile('test.log',{encoding: 'utf-8'});expect(content).toEqual('new-content');fs.writeFileSync('test.log','new-content-2');constsyncContent=awaitfs.promises.readFile('test.log',{encoding: 'utf-8'});expect(syncContent).toEqual('new-content-2');fs.createWriteStream('test.log').write('new-content-3');conststreamContent=awaitfs.promises.readFile('test.log',{encoding: 'utf-8'});expect(streamContent).toEqual('new-content-3');// Streaming (this is how winston is doing it?)constwriteStream=fs.createWriteStream('test.log');writeStream.on('open',()=>{// Write data to the filewriteStream.write('WriteStream Content');// Close the streamwriteStream.end();});constwriteStreamContent=awaitfs.promises.readFile('test.log',{encoding: 'utf-8'});expect(writeStreamContent).toEqual('WriteStream Content');mock.restore();
Now here is the Winston example, where I set the file transport and all, but somehow when I read the file, it still contains the old, mocked content, instead of the intended new, overwritten content:
constrootNodeModules=path.resolve(__dirname,'../../../../node_modules');mock({'test.log': 'content',[rootNodeModules]: mock.load(rootNodeModules)});constlogger=createLogger()// pseudo codeconstfileTransport=newtransports.File({filename: 'test.log',level: 'info'});logger.add(fileTransport)logger.error('logs error only in test.log because the level is less than info');constlog=awaitfs.promises.readFile('test.log',{encoding: 'utf-8'});expect(log).toMatch(/logs error only in test.log because the level is less than info/);// <---- here `log` still contains `content` instead of the new value.
What version of Winston presents the issue?
^3.15.0
What version of Node are you using?
v20.13.1
If this worked in a previous version of Winston, which was it?
No response
Minimum Working Example
No response
Additional information
No response
The text was updated successfully, but these errors were encountered:
🔎 Search Terms
mocking, mock-fs, async
The problem
When I mock away my file system, the read operation seems to happen before Winston starts writing the log.
With the latest version of mock-fs, they've changed on which event loop they operate, they switched from
process.nextTick()
tosetImmediate()
, which, as far as I understand the issue, should be fine.The reason I think this might be a winston issue, and not a mock-fs issue, is because I tried replicating the issue I am facing with many fs-based functions, which all seem to work the intended way, unlike winston.
Here is a minimal example of what I've tried with
fs
vs what I am trying to get working with Winston.Mocking and directly using
fs
functions:Now here is the Winston example, where I set the file transport and all, but somehow when I read the file, it still contains the old, mocked content, instead of the intended new, overwritten content:
What version of Winston presents the issue?
^3.15.0
What version of Node are you using?
v20.13.1
If this worked in a previous version of Winston, which was it?
No response
Minimum Working Example
No response
Additional information
No response
The text was updated successfully, but these errors were encountered: