Node v20.5.0 introduced an optimization to fs.readFileSync for utf-8 files. Unfortunately, this breaks mock-fs. Here is a minimal test case that works with Node v20.4.0 but breaks with Node v20.5.0:
import mockFs from "mock-fs";
import { test } from "node:test";
import { readFileSync } from "fs";
import assert from "node:assert/strict";
test("mockFs", () => {
mockFs({ "file": "contents" })
const fileContents = readFileSync("file", 'utf-8').toString()
assert.equal(fileContents, "contents")
})
In Node v20.5.0, I get the error Error: ENOENT: no such file or directory, open 'file'.
The above test case also works in Node v20.5.0 if I remove the 'utf-8' option from readFileSync. This issue specifically occurs for readFileSync with 'utf-8' option.
Here is the relevant commit: nodejs/node#48658
Node v20.5.0 introduced an optimization to
fs.readFileSyncfor utf-8 files. Unfortunately, this breaks mock-fs. Here is a minimal test case that works with Node v20.4.0 but breaks with Node v20.5.0:In Node v20.5.0, I get the error
Error: ENOENT: no such file or directory, open 'file'.The above test case also works in Node v20.5.0 if I remove the
'utf-8'option fromreadFileSync. This issue specifically occurs forreadFileSyncwith'utf-8'option.Here is the relevant commit: nodejs/node#48658