tschaub / mock-fs

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

Any plan to support readdir "withFileTypes" option? #272

Closed jzecca closed 4 years ago

jzecca commented 5 years ago

fast-glob uses this option since v3 in its "modern mode" to make fewer calls to file system.

Using it in combination with mock-fs leads to the following error:

Method not implemented

    at notImplemented (node_modules/mock-fs/lib/binding.js:140:9)
    at Object.<anonymous>.Binding.readdir (node_modules/mock-fs/lib/binding.js:890:5)
    at Object.<anonymous> (node_modules/mock-fs/lib/index.js:33:39)
    at readdirWithFileTypes (node_modules/@nodelib/fs.scandir/out/providers/async.js:15:17)
tannerntannern commented 5 years ago

I had to work around this in one of my projects, and in my case it was tolerable to simply disable "modern mode" by passing stats: true as an option to fast-glob, since modern mode is not used when the node version is <10.10, or stats is enabled.

Another option would be to detect if mocha is running and conditionally disable modern mode, so only your tests are affected by the performance hit, not the code your users run. Something like this would help you determine if mocha is running:

// utils.js
export const IS_MOCHA = !!process.env.MOCHA_COLORS;

You could also try detect-mocha, but I haven't used it myself.

jzecca commented 5 years ago

Thanks for the heads up, that's what I ended up doing. It's not ideal but it works :)

Native support would be awesome though.

bleucitron commented 5 years ago

I'm not sure to understand the workaround... Is fast-glob a dependency of fs ?

Then how do you pass stats: true to fast-glob if you don't have it installed as direct dependency in your project ?

jzecca commented 5 years ago

It's indeed only a workaround for usage with fast-glob.

If your code (or one of your dependencies) make use of the new Node 10.10+ fs.Dirent class, you'll face the same error that I got, since mock-fs doesn't support it yet.

mrmlnc commented 4 years ago

@tschaub, maybe it's time to release a new version?

tschaub commented 4 years ago

Fix from @mrmlnc published in mock-fs@4.11.0.

Thanks for the fix and for the nudge, @mrmlnc.