-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathvitest.setup.js
More file actions
118 lines (100 loc) · 2.84 KB
/
Copy pathvitest.setup.js
File metadata and controls
118 lines (100 loc) · 2.84 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import path from 'node:path';
import { vi } from 'vitest';
// Shared State
const { proxyState } = vi.hoisted(() => ({
proxyState: { useMemfs: false },
}));
// Flatten helper
function flatten(input, base = process.cwd()) {
const output = {};
for (const [key, value] of Object.entries(input)) {
const fullPath = path.resolve(base, key);
if (typeof value === 'string' || Buffer.isBuffer(value)) {
output[fullPath] = value;
} else if (typeof value === 'object') {
Object.assign(output, flatten(value, fullPath));
}
}
return output;
}
// Mock 'mock-fs'
vi.mock('mock-fs', async () => {
const { vol } = await import('memfs');
const nodeFs = await import('node:fs');
const os = await import('node:os'); // Import os
const mock = (config = {}) => {
proxyState.useMemfs = true;
vol.reset();
const flatConfig = flatten(config);
vol.fromJSON(flatConfig);
try {
vol.mkdirSync(process.cwd(), { recursive: true });
} catch {
// Ignore errors
}
try {
vol.mkdirSync(os.tmpdir(), { recursive: true });
} catch {
// Ignore errors
}
};
mock.restore = () => {
proxyState.useMemfs = false;
vol.reset();
};
mock.load = (filePath) => {
try {
return nodeFs.readFileSync(filePath, 'utf-8');
} catch {
return `<Error loading ${filePath}>`;
}
};
return { default: mock };
});
// Proxy Factory
async function createFsProxy(moduleName) {
const actual = await vi.importActual(moduleName);
const { fs: memfs } = await import('memfs');
const exports = { ...actual };
// Helper to determine implementation
const getImpl = (key) => {
// If mocking is OFF, use Real FS
if (!proxyState.useMemfs) return actual[key];
// If mocking is ON, use Memfs...
if (key in memfs) return memfs[key];
// ...BUT if Memfs is missing the method, THROW (Don't leak to disk!)
return () => {
throw new Error(
`❌ Test Error: Method 'fs.${key}' is not supported by memfs, and we blocked a real disk write.`,
);
};
};
// Wrap every method
for (const key of Object.keys(actual)) {
if (typeof actual[key] === 'function') {
exports[key] = function (...args) {
return getImpl(key).apply(this, args);
};
}
}
// Wrap default export
exports.default = new Proxy(actual.default, {
get: (_, prop) => getImpl(prop),
});
return exports;
}
// Apply Mocks
vi.mock('node:fs', () => createFsProxy('node:fs'));
vi.mock('fs', () => createFsProxy('fs'));
vi.mock('fs/promises', async () => {
const actual = await vi.importActual('node:fs/promises');
const { fs: memfs } = await import('memfs');
return new Proxy(actual, {
get(target, prop) {
if (proxyState.useMemfs && memfs.promises[prop]) {
return memfs.promises[prop];
}
return Reflect.get(target, prop);
},
});
});