Skip to content

doMock doesn't mock fs in the graceful-fs package #9343

@daiyam

Description

@daiyam

Describe the bug

Hello,

I'm mocking fs to be used in a tested function.
The function is using fs-extra to read some files.

After testing, I found out that the issue is with graceful-fs (also used by fs-extra).

import fs from 'fs'
import gfs from 'graceful-fs'; 

export async function testFunction(filePath: string): Promise<void> {
  fs.readFile(filePath, 'utf8', (error, data) => console.log(error ?? data));
  // get the content of the mocked file

  gfs.readFile(filePath, 'utf8', (error, data) => console.log(error ?? data));
  // get: Error: ENOENT: no such file or directory, open '/incoming/package.json'

  ...
}

Reproduction

I've made the test project: https://github.com/daiyam/issues/tree/master/vitest/9343 which replicate the issue.

System Info

System:
    OS: macOS 15.3.1
    CPU: (10) arm64 Apple M4
    Memory: 255.73 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.21.1 - /Users/baptiste/.nvm/versions/node/v22.21.1/bin/node
    Yarn: 1.22.22 - /Users/baptiste/.nvm/versions/node/v14.21.3/bin/yarn
    npm: 7.24.2 - /Volumes/Data/Development/Projects/github.com/zokugun/artifact/node_modules/.bin/npm
    bun: 1.3.0 - /Users/baptiste/.bun/bin/bun
  Browsers:
    Brave Browser: 143.1.85.117
    Chrome: 143.0.7499.170
    Firefox: 146.0
    Safari: 18.3
  npmPackages:
    vitest: ^4.0.16 => 4.0.16

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions