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

Add an overlay option #142

Open
tschaub opened this issue Jul 10, 2016 · 6 comments
Open

Add an overlay option #142

tschaub opened this issue Jul 10, 2016 · 6 comments

Comments

@tschaub
Copy link
Owner

tschaub commented Jul 10, 2016

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
Copy link

that would satisfy my main use case.

@joeyespo
Copy link

joeyespo commented Jan 4, 2017

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

This would solve my case quite nicely as well.

@vlindhol
Copy link
Contributor

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

@papandreou
Copy link

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
Copy link

half-full-or-twice-too-large commented Aug 7, 2017

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
Copy link

cdibbs commented Aug 10, 2017

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.

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

7 participants