jest-plugin-fs
Jest plugin for mocking out the filesystem.
Last updated 3 years ago by negativetwelve .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install jest-plugin-fs 
SYNC missed versions from official npm registry.

jest-plugin-fs

npm npm npm

Jest plugin for mocking out the filesystem.

Getting Started

Install jest-plugin-fs using yarn:

yarn add --dev jest-plugin-fs

Motivation

Jest currently does not have an easy way to mock out the filesystem. This plugin aims to change that. Here's an example:

import fs from 'jest-plugin-fs';

// Mock out the filesystem.
jest.mock('fs', () => require('jest-plugin-fs/mock'));

describe('FileWriter', () => {
  // Create an in-memory filesystem.
  beforeEach(() => fs.mock());
  afterEach(() => fs.restore());

  describe('.write', () => {
    set('filename', () => 'path/to/my/file');
    action('write', () => FileWriter.write(filename));

    it('should write a new file', () => {
      expect(write).not.toThrow();
    });

    describe('resulting file', () => {
      beforeEach(() => write());

      it('should create the new file', () => {
        expect(fs.readFileSync(filename)).toEqual('new-file');
      });
    });
  });
});

Usage

If you want, you can import fs from jest-plugin-fs at the top of every test:

import fs from 'jest-plugin-fs';

// This installs the mock for 'fs'.
jest.mock('fs', () => require('jest-plugin-fs/mock'));

If you want to install fs attached to the global jest object, you can modify the jest section of your package.json to include:

"jest": {
  "setupFiles": [
    "jest-plugin-fs/setup"
  ]
}

Example

Here's an example test that tests fs itself:

// TODO

Current Tags

  • 2.9.0                                ...           latest (3 years ago)

24 Versions

  • 2.9.0                                ...           3 years ago
  • 2.8.2                                ...           3 years ago
  • 2.8.1                                ...           3 years ago
  • 2.8.0                                ...           3 years ago
  • 2.7.0                                ...           3 years ago
  • 2.6.0                                ...           3 years ago
  • 2.5.1                                ...           3 years ago
  • 2.5.0                                ...           3 years ago
  • 2.4.2                                ...           3 years ago
  • 2.4.1                                ...           3 years ago
  • 2.4.0                                ...           3 years ago
  • 2.3.4                                ...           3 years ago
  • 2.3.3                                ...           3 years ago
  • 2.3.2                                ...           3 years ago
  • 2.3.1                                ...           3 years ago
  • 2.3.0                                ...           3 years ago
  • 2.2.4                                ...           3 years ago
  • 2.2.3                                ...           3 years ago
  • 2.2.2                                ...           3 years ago
  • 2.2.1                                ...           3 years ago
  • 2.2.0                                ...           3 years ago
  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.8.5                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 6
Last Day 0
Last Week 0
Last Month 12
Dependencies (1)
Dev Dependencies (2)

Copyright 2014 - 2016 © taobao.org |