|
1 | | -import fs from 'fs'; |
2 | | -import mockFs from 'mock-fs'; |
| 1 | +import { vol, fs as memfs } from 'memfs'; |
| 2 | + |
| 3 | +// Mock the built-in 'fs' module so all fs calls inside prebuild use memfs |
| 4 | +jest.mock('fs', () => memfs); |
| 5 | + |
| 6 | +// Mock prettier to avoid triggering dynamic import inside Jest VM (Prettier v3 ESM) |
| 7 | +jest.mock('prettier', () => ({ |
| 8 | + format: (code: string) => code, |
| 9 | +})); |
| 10 | + |
3 | 11 | import { createComponentNameOutput, componentNameOutputOptions } from './prebuild'; |
4 | 12 |
|
5 | 13 | describe('prebuild', () => { |
6 | | - it('should read out component folders', async () => { |
7 | | - mockFs({ |
8 | | - 'src/components': { |
9 | | - 'post-accordion': {}, |
10 | | - 'post-avatar': {}, |
11 | | - 'post-back-to-tlop': {}, |
12 | | - 'post-breadcrumbs': {}, |
13 | | - 'post-breadcrumbs-item': {}, |
14 | | - 'post-123-component': {}, |
15 | | - 'some-other-folder': {}, |
16 | | - 'folder_name_with_different_notation': {}, |
| 14 | + beforeEach(() => { |
| 15 | + vol.reset(); |
| 16 | + // Create directory structure; null values create directories in vol.fromJSON |
| 17 | + vol.fromJSON( |
| 18 | + { |
| 19 | + 'folder_name_with_different_notation': '', |
| 20 | + 'post-123-component': '', |
| 21 | + 'post-accordion': '', |
| 22 | + 'post-avatar': '', |
| 23 | + 'post-back-to-tlop': '', |
| 24 | + 'post-breadcrumbs': '', |
| 25 | + 'post-breadcrumbs-item': '', |
| 26 | + 'some-other-folder': '' |
17 | 27 | }, |
18 | | - 'src/_generated': {}, |
19 | | - }); |
| 28 | + 'src/components/', |
| 29 | + ); |
| 30 | + }); |
20 | 31 |
|
21 | | - await createComponentNameOutput(componentNameOutputOptions); |
| 32 | + afterEach(() => { |
| 33 | + vol.reset(); |
| 34 | + }); |
22 | 35 |
|
23 | | - const componentNamesJson = fs.readFileSync('src/_generated/component-names.json', 'utf8'); |
24 | | - const componentNamesScss = fs.readFileSync('src/_generated/component-names.scss', 'utf8'); |
| 36 | + it('should read out component folders', async () => { |
| 37 | + await createComponentNameOutput(componentNameOutputOptions); |
25 | 38 |
|
26 | | - mockFs.restore(); |
| 39 | + const componentNamesJson = memfs.readFileSync('src/_generated/component-names.json', 'utf8'); |
| 40 | + const componentNamesScss = memfs.readFileSync('src/_generated/component-names.scss', 'utf8'); |
27 | 41 |
|
28 | 42 | expect(componentNamesJson).toMatchSnapshot(); |
29 | 43 | expect(componentNamesScss).toMatchSnapshot(); |
|
0 commit comments