tschaub / mock-fs

Configurable mock for the fs module
https://npmjs.org/package/mock-fs
Other
909 stars 86 forks source link

`fs.cp` mock throws `ENOENT` #403

Open vlukashov opened 6 days ago

vlukashov commented 6 days ago

It looks like the fs.cp mock does not work on Node 20.

In my examples fs.stat('/path/that/exists/only/in/mock-fs') works indicating that mocks are active, yet fs.cp('/path/that/exists/only/in/mock-fs', tempPath, {recursive: true}) throws.

Example (index.js file):

import { default as mock } from "mock-fs";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

async function statAndCopy(source, dest) {
  const stats = await fs.stat(source);
  console.log(source, stats.ctime);
  await fs.cp(source, dest, { recursive: true });
}

const tempdirs = [];
try {
  console.log(`native:`);
  tempdirs.unshift(await fs.mkdtemp(os.tmpdir()));
  await statAndCopy(__dirname, tempdirs[0]);
  console.log();

  mock({ "/source": mock.load(`${__dirname}`) });

  console.log(`mocked:`);
  tempdirs.unshift(await fs.mkdtemp(os.tmpdir()));
  await statAndCopy("/source", tempdirs[0]);
  console.log();

  mock.restore();
} catch (e) {
  console.error(e);
} finally {
  await Promise.allSettled(tempdirs.map((d) => fs.rm(d, { recursive: true })));
}

arm64, mac OS 14.7, node v20.15.0, mock-fs@5.4.1

vlukashov commented 6 days ago

Could be related: https://github.com/tschaub/mock-fs/issues/358? Please feel free to close this issue if it's a duplicate.