fixtures-fs

Create a temporary fs with JSON fixtures

Usage no npm install needed!

<script type="module">
  import fixturesFs from 'https://cdn.skypack.dev/fixtures-fs';
</script>

README

fixtures-fs

build status Coverage Status Davis Dependency status

NPM

Create a temporary fs with JSON fixtures

Example

var withFixtures = require('fixtures-fs');
var test = require('tape');
var path = require('path');

var myNpmVerify = ...

/*
  this test wants a directory set up with some mock data
  then do asserts against the directory with some code.

  withFixtures creates the file system before your test and 
  tears it down after your test is done.
*/
test('something something npm', withFixtures(__dirname, {
  foo: {
    'package.json': JSON.stringify({
      name: 'foo',
      version: 'bar',
      dependecies: { foobaz: '~1.0.0' }
    }),
    'npm-shrinkwrap.json': JSON.stringify({
      name: 'foo',
      version: 'bar',
      dependencies: {
        foobaz: {
          version: '3.0.0',
          resolved: 'http://npm.registry.org/foobaz/foobaz-3.0.0.tgz'
        }
      }
    })
  }
}, function (assert) {
  myNpmVerify(path.join(__dirname, 'foo'), function (err) {
    assert.equal(err.message, 'shrinkwrap is wrong.');

    assert.end();
  });
}));

example with mocha

var withFixtures = require('fixtures-fs');
var suite = require('mocha').suite;
var test = require('mocha').it;
var assert = require('assert');
var path = require('path');

var myNpmVerify = ...

suite('test npm stuff', function () {
  test('something something npm', withFixtures(__dirname, {
    ...
  }, function (end) {
    myNpmVerify(path.join(__dirname, 'foo'), function (err) {
      assert.equal(err.message, 'shrinkwrap is wrong.');

      end();
    });
  }));
});

Docs

var func = withFixtures(dirname, fixtures, lambda, opts={})

fixtures-fs := (
    dirname: String,
    fixtures: Fixture, 
    lambda: (EndCallback<T>) => void,
    opts?: FsMock
) => (EndCallback<T>) => void

withFixtures takes a dirname, an object of fixtures and an async lambda function

It will run create a file system matching your fixtures before calling your async lambda function and tear down the file system once the lambda finishes.

You can invoke the returned func with a callback or an object with an .end() method.

dirname

This is the directory the fixtures will be written into.

If this directory does not exist, it will be created.

fixtures

fixtures := Object<String, String | Fixture>

The fixtures object has key value pairs where the key is the directory or file name and the value is either a string content of the file or another object that is the content of a directory

lambda

lambda := (Callback<Error, T> | Object & {
    end: Callback<Error, T>
}) => void

The lambda should be an async function that takes either a callback or an object with an .end(err=null) method. This callback / .end() method is intercepted and withFixtures will cleanup the file system before it completes.

opts

opts := { fs: Object, mkdirp: Function, rimraf: Function }

You can pass an optional opts object to customize the file system functions that withFixtures uses. This is useful if you want to withFixtures to write to a non-standard file system like a browser file system, a remote file system or an in memory file system.

func

The func result of the withFixtures() call can be passed directly into the test() function of tape or the test() function of mocha.

When you call func with either a callback or object with an .end() method argument it will create the fixtures, invoke the lambda, tear down the fixtures and then invoke either the callback or .end() method.

Installation

npm install fixtures-fs

Tests

npm test

Contributors

  • Raynos

MIT Licenced