|
1 | 1 | import { vi } from 'vitest'; |
2 | 2 |
|
3 | | -const createMemFs = vi.hoisted( |
4 | | - () => async (_fsOriginal: typeof import('node:fs')) => { |
| 3 | +const createUnionFs = vi.hoisted( |
| 4 | + () => async (fsOriginal: typeof import('node:fs')) => { |
5 | 5 | const { Volume, createFsFromVolume } = await import('memfs'); |
| 6 | + const { ufs } = await import('unionfs'); |
6 | 7 |
|
7 | | - return createFsFromVolume(Volume.fromJSON({})); |
| 8 | + const memFs = createFsFromVolume(Volume.fromJSON({})); |
| 9 | + const union = ufs.use(memFs as never).use(fsOriginal as never); |
| 10 | + |
| 11 | + if (union.promises && typeof fsOriginal.promises?.rm === 'function') { |
| 12 | + const memFsPromises = ( |
| 13 | + memFs as { |
| 14 | + promises?: { rm?: (path: string, options?: object) => Promise<void> }; |
| 15 | + } |
| 16 | + ).promises; |
| 17 | + ( |
| 18 | + union.promises as { |
| 19 | + rm: (path: string, options?: object) => Promise<void>; |
| 20 | + } |
| 21 | + ).rm = async (path, options) => { |
| 22 | + if (typeof memFsPromises?.rm === 'function') { |
| 23 | + try { |
| 24 | + return await memFsPromises.rm(path, options); |
| 25 | + } catch { |
| 26 | + return await fsOriginal.promises.rm(path, options); |
| 27 | + } |
| 28 | + } |
| 29 | + return await fsOriginal.promises.rm(path, options); |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + return union; |
8 | 34 | } |
9 | 35 | ); |
10 | 36 |
|
11 | 37 | vi.mock('node:fs', async (importOriginal) => { |
12 | 38 | return { |
13 | | - default: await createMemFs( |
| 39 | + default: await createUnionFs( |
14 | 40 | await importOriginal<typeof import('node:fs')>() |
15 | 41 | ), |
16 | 42 | }; |
17 | 43 | }); |
18 | 44 |
|
19 | 45 | vi.mock('fs', async (importOriginal) => { |
20 | 46 | return { |
21 | | - default: await createMemFs(await importOriginal<typeof import('fs')>()), |
| 47 | + default: await createUnionFs(await importOriginal<typeof import('fs')>()), |
22 | 48 | }; |
23 | 49 | }); |
0 commit comments