Wondering what’s next for npm?Check out our public roadmap! »

jest-plugin-fsTypeScript icon, indicating that this package has built-in type declarations

2.2.2 • Public • Published


npm npm npm

Jest plugin for mocking out the filesystem.

Getting Started

Install jest-plugin-fs using yarn:

yarn add --dev jest-plugin-fs


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

describe('FileWriter', () => {
  beforeEach(() => jest.fs.create({}));
  afterEach(() => jest.fs.restore());
  describe('.write', () => {
    set('filename', () => 'path/to/my/file');
    action('write', () => FileWriter.write(filename));
    it('should write a new file', () => {
    describe('resulting file', () => {
      beforeEach(() => write());
      it('should create the new file', () => {


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

import fs from 'jest-plugin-fs';

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

  "setupFiles": [


Here's an example test that tests fs itself: