by snowmantw

snowmantw / Fe

Mock file and directories in Node.js

122 Stars 2 Forks Last release: Not found 15 Commits 1 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

Fe - mock filesystem in the easist way

Create mock files and directories in the easiest way:

var fe = require('fe.js'),
    fs = fe.fs;

// Create a 'directory'. var bar = fe.instance().directory('/foo/bar');

// Create a 'file'. var charlie = fe.instance().file(bar, 'charlie.json', {'delta': 3.14}, function(oldContent, content, mode) {

// This function can return handled content before it got read.
// You can even convert it as binary data to fit your reader function.
if ('r' === mode)
  return oldContent.delta;


// Read the file through the path. Would print '3.14'. console.log( fs.readFileSync('/foo/bar/charlie.json') );

The mocked

module can be used with
, which can substitute the original
module in the test context:
// You can find this in '/test/features/supports/world.js'
// Proxyquire: https://github.com/thlorenz/proxyquire
var proxyquire = require('proxyquire')
  , assert     = require('assert')
  , fe         = require( __dirname + '/../../../exports.js')
  , fsStub     = fe.fs
  , wrench = proxyquire('wrench', { 'fs': fsStub });

// Now the fs module in the wrench would be replaced with our // mocked one. It would access only the Fe's "filesystem".


npm install fe.js


to get the package, which contains the mock filesystem instance and the
module. To use them:
var fe = require('fe.js'),
    fs = fe.fs;

Now you can mock files and directories in

, or manipulate it with the mock

Don't forget that you can use

to stub existing modules with the mock
var proxyquire = require('proxyquire')
  , fsStub     = fe.fs
  , wrench = proxyquire('wrench', { 'fs': fsStub });


Sometime we just want to test some simple file handling functions, and don't want to create real files and directories because:

  • It's annoying to remember to remove test files and directories after tests

  • It's annoying to fill the content of dummy files with file APIs

  • Forgot to delete files after test may cause some troubles

  • Tests should completely live within sandbox. Create files and directories may cause side-effects on your system

  • If your dummy files and directories got affected by other processes on your system, you may have no chance to know what's going wrong with your tests

So I create this to implement a filesystem in process memory, and a mocked

module which has some compatible APIs.

Unsupported APIs

Right now, some APIs in the

can't be supported due to the complexities. Some of them are:
  • write/read: have no idea about how to support buffer
  • truncate/ftruncate: have no idea about how to handle length
  • link/symlink: the way I used is hard to create and maintain links

Others are maybe supportable. But I'm lack of time to implement them so far. So if you want to support more APIs, feedbacks and even patches are welcome.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.