title: Jest - Mocking and testing the node.js filesystem
body: <p>I have created a function which basically loops over an array and create files. I'm starting to get into testing using Jest to have some extra security in place to make sure everything works however I'm experiencing some issues trying to mock the Node.js filesystem.</p>
  
  <p>This is the function I wish to test - <strong>function.ts</strong>:</p>
  
  <pre><code>export function generateFiles(root: string) {
    fs.mkdirSync(path.join(root, '.vscode'));
    files.forEach((file) =&gt; {
      fs.writeFileSync(
        path.join(root, file.path, file.name),
        fs.readFileSync(path.join(__dirname, 'files', file.path, file.name), 'utf-8')
      );
    });
  }
  
  const files = [
    { name: 'tslint.json', path: '' },
    { name: 'tsconfig.json', path: '' },
    { name: 'extensions.json', path: '.vscode' },
  ];
  </code></pre>
  
  <p>I've been reading around but can't really figure out how to test this with jest. No examples to look at. I've tried to install <code>mock-fs</code> which should be a simple way of getting up and running with a mock version of the Node.js FS module but I honestly don't know where to start. This is my first attempt at making a simple test - which causes an error, says 'no such file or directory' - <strong>function.test.ts</strong>:</p>
  
  <pre><code>import fs from 'fs';
  import mockfs from 'mock-fs';
  
  beforeEach(() =&gt; {
    mockfs({
      'test.ts': '',
      dir: {
        'settings.json': 'yallo',
      },
    });
  });
  
  test('testing mock', () =&gt; {
    const dir = fs.readdirSync('/dir');
    expect(dir).toEqual(['dir']);;
  });
  
  afterAll(() =&gt; {
    mockfs.restore();
  });
  </code></pre>
  
  <p>Anyone who can point me in the right direction?</p>