-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest-setup.ts
More file actions
30 lines (26 loc) · 997 Bytes
/
vitest-setup.ts
File metadata and controls
30 lines (26 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { beforeEach, vi } from "vitest";
import { vol, fs } from "memfs";
import mockRequire from "mock-require";
import Module from "node:module";
import path from "node:path";
import "@testing-library/jest-dom/vitest";
import "./src/testing/internal/matchers";
vi.mock("node:fs");
vi.mock("node:fs/promises");
// Intercept CJS require("fs") in externalized node_modules (cosmiconfig) since
// vitest only mocks ESM-compatible imports
mockRequire("fs", fs);
mockRequire("fs/promises", fs.promises);
// Mock import-fresh to read from memfs instead of real filesystem.
// Cosmiconfig uses import-fresh to load .js/.cjs config files via require(),
// which bypasses the mocked fs.
mockRequire("import-fresh", (filepath: string) => {
const content = fs.readFileSync(filepath, "utf-8");
const m = new Module(filepath);
m.paths = (Module as any)._nodeModulePaths(path.dirname(filepath));
(m as any)._compile(content, filepath);
return m.exports;
});
beforeEach(() => {
vol.reset();
});