tschaub / mock-fs

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

Add an overlay option #142

Open tschaub opened 8 years ago

tschaub commented 8 years ago

An overlay option would make it so the fs module operates on the in-memory filesystem for all mocked paths and operates on the real file system for all other paths.

For example:

var assert = require('assert');
var mock = require('mock-fs');
var fs = require('fs');

var config = {
  'path/to/mock-file.txt': 'file content'
};

assert.throws(() => fs.accessSync('path/to/mock-file.txt'));
assert.doesNotThrow(() => fs.accessSync(__filename));

mock(config);
assert.doesNotThrow(() => fs.accessSync('path/to/mock-file.txt'));
assert.throws(() => fs.accessSync(__filename));
mock.restore();

mock(config, {overlay: true});
assert.doesNotThrow(() => fs.accessSync('path/to/mock-file.txt'));
assert.doesNotThrow(() => fs.accessSync(__filename));
mock.restore();
amwmedia commented 8 years ago

that would satisfy my main use case.

joeyespo commented 7 years ago

Is this still the direction you want to take it? Any code written yet?

This would solve my case quite nicely as well.

vlindhol commented 7 years ago

Seconding this! I'm having problems mixing proxyquire and mock-fs, this would solve it!

papandreou commented 7 years ago

I'll need this to keep fileception working with mock-fs 4. Or I'd need the require('mock-fs').fs(...) feature reintroduced.

half-full-or-twice-too-large commented 7 years ago

Hello,

Great tool and would love to use it, but can't. Below code doesn't work if I comment out line 1. There are other examples where a library I use attempts to lazy load its dependencies after file system was mocked for my test. Would greatly appreciate a built-in ability to use mock-fs with such libraries.

var set             = require ('lodash/set');

var csv             = require ('csvtojson')();
var mockfs          = require ('mock-fs');          

let mockfileO = {};
mockfileO[`${process.cwd()}\\test.csv`] =   
    'header1,header2\n' + 
    'data1-1,data2-1\n' + 
    'data1-2,data2-2\n';

mockfs (mockfileO);

let stream = require ('csvtojson')().fromFile (`${process.cwd()}\\test.csv`)
    .on ('error', (err)=>{ throw new Error(err); })
    .on ('end', mockfs.restore );

stream.pipe (process.stdout);
cdibbs commented 7 years ago

Like many others, I've run into this as a show-stopper, too. Need .fs() or this issue.

Full integration tests in particular must have access to the original file system.