title: Testing the file system with Jest?
body: <p>I'm using <a href="https://github.com/tschaub/mock-fs" rel="noreferrer"><code>mock-fs</code></a> to try and test a Webpack plugin I wrote, which modifies a file on my file system.</p>
  
  <p>Here's the test:</p>
  
  <pre><code>test('writes chunks to build/assets.json if no json file present', () =&gt; {
     mockFs({
      [buildDir]: {},
    });
  
    const stats = new Stats({
      assetsByChunkName: {
        main: 'main.somecrazyhash12341213445345.js',
      },
    });
    const compiler = new Compiler(stats);
    const plugin = new ChunksToJsonPlugin(config);
  
    expect(fs.existsSync(assetFilePath)).toBe(false);
  
    plugin.apply(compiler);
    compiler.execHandler();
  
    expect(fs.existsSync(assetFilePath)).toBe(true);
    expect(fs.readFileSync(assetFilePath, 'utf-8')).toEqual(
      JSON.stringify({
        main: 'main.somecrazyhash12341213445345.js',
      })
    );
  
    mockFs.restore();
  });
  </code></pre>
  
  <p>It works beautifully when I run it by itself, but when I run it as part of a suite, other tests (that don't use <code>mock-fs</code>) break.</p>
  
  <p><a href="http://d.pr/i/z2ne+" rel="noreferrer">ss http://d.pr/i/z2ne+</a></p>
  
  <p>I notice that <code>mock-fs</code> is in the stacktrace, which leads me to believe that the file system is being mocked in those tests too (which I don't want).</p>
  
  <p><code>mock-fs</code> states that:</p>
  
  <blockquote>
    <p>The mock-fs@4 release will contain breaking changes. Instead of overriding all methods of the built-in <code>fs</code> module, the library now overrides <code>process.binding('fs')</code>. The purpose of this change is to avoid conflicts with other libraries that override <code>fs</code> methods (e.g. <code>graceful-fs</code>) and to make it possible to work with multiple Node releases without maintaining copied and slightly modified versions of Node's <code>fs</code> module.</p>
  </blockquote>
  
  <p>I don't know enough about how <code>process.binding</code> works, especially as it relates to Jest running tests in parallel, but I feel like this is the core issue.</p>
  
  <p>How can I make this work? Is there another way to test this behavior without using <code>mock-fs</code>?</p>