Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a Volume from a real directory #959

Open
regseb opened this issue Oct 23, 2023 · 1 comment
Open

Create a Volume from a real directory #959

regseb opened this issue Oct 23, 2023 · 1 comment

Comments

@regseb
Copy link
Contributor

regseb commented Oct 23, 2023

For my unit tests, I'd like to create a virtual file system with a single file and also the directory node_modules/. My program has a dependency that:

  • takes a file name as parameter;
  • reads this file;
  • and also reads files from its node_modules/ directory (e.g. configuration files).

With mock-fs, I use mock.load():

mock({
  'foo.txt': 'bar',
  'node_modules/': mock.load('node_modules/'),
});

I'd like to switch to memfs (with fs-monkey), but I can't add the node_modules/ directory to the virtual file system.


I suggest adding the Volume.fromDirectory() function to create a volume from a real directory:

import { Volume } from 'memfs';
import { Union } from 'unionfs';
import { patchFs } from 'fs-monkey';

patchFs(new Union()
  .use(Volume.fromJSON({ 'foo.txt': 'bar' }))
  .use(Volume.fromDirectory('node_modules/')));
@manferlo81
Copy link

same problem here, I am trying to replace mock-fs as it hasn't been updated in 2 year and it has a bug with NodeJS >=20 (not working with readFileSync & writeFileSync... maybe others) but I am having a hard time finding a solution formthis very issue.

I need to add a "clone" (let's call it a clone) of my node_modules folder into my volume as some of my dependencies will look for some modules during test. I could reinvent the wheel and write a function to read my "real" node_modules and add it to my volume but I think this is a functionality memfs should have, maybe I am missing something and there is a way, thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants