• Stars
    star
    121
  • Rank 292,258 (Top 6 %)
  • Language
    JavaScript
  • Created almost 11 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Mock file and directories in Node.js

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.

More Repositories

1

Fluorine

Context-based Javascript eDSL and library; provides a way to isolate impure computations.
JavaScript
40
star
2

node-webkit-test

JavaScript
17
star
3

lightdm-webkit-Y-combinator

(Y F) = (F (Y F))
JavaScript
17
star
4

Frag

Porting from Darcs for collecting issues and other informations. Based on frag-1.1.2b.tar-0001.gz .
Haskell
13
star
5

espect.js

A static AOP tool for ECMAScript
JavaScript
6
star
6

react-bandjs-renderer

Render sound via Band.js to show renderer is just an effect engine
JavaScript
4
star
7

rune.js

An eDSL utilitiy
JavaScript
3
star
8

docker-neo4j

Neo4j container template
Shell
2
star
9

docker-cassandra

Docker container. Cassandra server with salt.
Shell
2
star
10

haproxy-templating

A HAProxy configure templating tool.
Python
2
star
11

Fluorine.arrow

Porting Arrow in Haskell to Javascript with event-driven features.
JavaScript
2
star
12

threedlibrary

**Mirror from Google Codes** : for bower or other js library manager.
JavaScript
2
star
13

ncu-es-materials-2016

Erlang
1
star
14

functw

Website for Taiwan Functors.
CSS
1
star
15

snowmantw.github.com

cat /dev/life >> /dev/repo
JavaScript
1
star
16

snowmantw-blogsource

Source files of snowmantw.github.com
1
star
17

restfs

Toy FUSE fs for RESTful service, in Rust + Python
Rust
1
star
18

lxc-restful

RESTful APIs for LXC container management.
JavaScript
1
star
19

ss-at1

SocialStudy AT-1 Project
JavaScript
1
star
20

g0vww

Taiwan g0vww project. Implement it as I promised.
JavaScript
1
star
21

aether

Aether: generate Gaia builds.
1
star
22

gleipnir.js

A library and abstract framework try to conquer common troubles of UI programming
JavaScript
1
star