Need help with Fe?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

snowmantw
122 Stars 2 Forks 15 Commits 2 Opened issues

Description

Mock file and directories in Node.js

Services available

!
?

Need anything else?

Contributors list

# 174,771
Vim
CSS
xmpp
React N...
11 commits

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

fs
module can be used with
proxyquire
, which can substitute the original
fs
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".

Installation

npm install fe.js

Require

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

Now you can mock files and directories in

fe.instance()
, or manipulate it with the mock
fs
module.

Don't forget that you can use

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

Motivation

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

fs
module which has some compatible APIs.

Unsupported APIs

Right now, some APIs in the

fs
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.