tschaub / mock-fs

Configurable mock for the fs module
https://npmjs.org/package/mock-fs
Other
906 stars 86 forks source link

createWriteStream is not mocked in node 16/17 #343

Closed BuZZ-T closed 2 years ago

BuZZ-T commented 2 years ago

Hi! I observed the following problem using mock-fs:

I've written a file using fs.createWriteStream: https://github.com/BuZZ-T/rusted-chromium/blob/35bd03817439545ec52c66a4a75d93acb13eb477/download.ts#L87

const file = createWriteStream(downloadPath + '.zip')
// ...
zipFileResponse.body.pipe(file)

and checked if it's written properly using mock-fs: https://github.com/BuZZ-T/rusted-chromium/blob/35bd03817439545ec52c66a4a75d93acb13eb477/download.int.spec.ts#L113

expect(existsSync(chromeZip10)).toBe(true)

This works on node 14. When running the tests with node 16 or 17, the tests fail: https://app.travis-ci.com/github/BuZZ-T/rusted-chromium/builds/241287559

When i run the tests locally with node 16 and 17, i can see that the file is created in the real filesystem. The functionality with the unmocked createWriteStream is working properly and the same for all tested node versions.

Am i doing sth. wrong, or is this a bug of mock-fs?

Thx for your help and your great work!

3cp commented 2 years ago

I deleted my previous reply, after I realised you said the file was created in real file system. It's bit odd, I checked our unit test code, it seems createWriteStream is creating mocked file just fine in node v16.

3cp commented 2 years ago

Can you check node 14, whether your test code generates file in real file system too?

3cp commented 2 years ago

What I guess is that your async downloadChromium function didn't wait for the write steam to finish writing. It might be the similar timing issue as createReadSteam in nodejs v16+ https://github.com/tschaub/mock-fs/issues/338#issuecomment-962393651

You probably can try using the write stream's "close" or "finish" event callback to mark the ending of downloadChromium.

BuZZ-T commented 2 years ago

Hi 3cp! No, using node 14 no file is created in the real fs.

Thx for the hint with the timing issue, i'll check that!

BuZZ-T commented 2 years ago

Sorry for the late feedback. @3cp was right, error was on my side. It's working now! I appreciate the support and your work!