Skip to content

Create a Volume from a real directory #959

@regseb

Description

@regseb

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/')));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions