Kevin Schaul

Visual journalist/hacker

Mock fs with vitest and memfs

June 26, 2024

I’ve been using vitest for testing node code lately, and I often want to set up a fake file system. I’d been using mockFS for this, which is no longer maintained. Fortunately memfs is a drop-in alternative — if you know this secret: You’ve got to add a mock fs call.

import { vol, fs } from "memfs";
import { vi, describe, test, expect, beforeEach, afterEach } from "vitest";

// Mock fs everywhere else with the memfs version.
vi.mock("fs", async () => {
  const memfs = await vi.importActual("memfs");

  // Support both `import fs from "fs"` and "import { readFileSync } from "fs"`
  return { default: memfs.fs, ...memfs.fs };
});

Then, you can set up a fake filesystem for your test:

describe('my-suite', () => {
  afterEach(() => {
    vol.reset();
  });

  ...

  test('same dir', async () => {
    vol.fromNestedJSON({
      '/Users/username/my-package': {
        'package.json': '{}',
        styles: {
          subdir: {
            'package.json': '{}',
          },
        },
      },
    });

    ...
  });
})

Thank you so much for this comment, bcass.