How to mock file system with memfs in NodeJS

1.1k Views Asked by At

I want to test the following function with the memfs package

import fs from 'fs-extra'
import path from 'path'

async function ensureParent(filepath: string) {
  const absolutePath = path.resolve(filepath)
  const parentDir = path.dirname(absolutePath)

  const exists = await fs.pathExists(parentDir)

  return exists
}

export default {
  ensureParent,
}

To test it with an in-memory file system I have at the root of the project, the following __mocks__/fs.ts file

import { fs } from 'memfs'

export default fs

and the following __mocks__/fs/promises.ts file

import { fs } from 'memfs'

export default fs.promises

Here is the test file file.test.ts

import { describe, it, expect, vi, beforeAll, beforeEach } from 'vitest'
import { vol } from 'memfs'

import f from '../file'

vi.mock('fs')
vi.mock('fs/promises')

const fileSystem = {
  './exists/shiman.txt': 'Hello shiman',
}

beforeAll(() => {
  vol.fromJSON(fileSystem, '/tmp')
})

beforeEach(() => {
  vol.reset()
})

describe.concurrent('file utils', () => {
  it('ensure a directory exists', async () => {
    expect(await f.ensureParent('/tmp/exists/shiman.txt')).toBe(true)
  })
})

However, the test keeps failing, and I realized that, although the mock is taken into account, the in-memory file system is not because await fs.readdir('/', (err, data) => console.log(err, data)) logs the content of / in my machine.

Have I done something wrong?

1

There are 1 best solutions below

0
On

In case others stumble upon this, this is what worked for me (for mocking fs/promises with memfs in vitest):

import { type fs, vol } from 'memfs'

// ...

vi.mock('node:fs/promises', async () => {
  const memfs: { fs: typeof fs } = await vi.importActual('memfs')

  return memfs.fs.promises
})

No files in __mocks__.