Skip to content

Commit 85fbcbf

Browse files
committed
refactor: fix mocking in tests
1 parent f0b9c6b commit 85fbcbf

File tree

3 files changed

+96
-94
lines changed

3 files changed

+96
-94
lines changed

packages/core/src/lib/implementation/read-rc-file.unit.test.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,30 @@ import { CONFIG_FILE_NAME, type CoreConfig } from '@code-pushup/models';
33
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
44
import { autoloadRc } from './read-rc-file.js';
55

6-
// mock bundleRequire inside importEsmModule used for fetching config
7-
vi.mock('bundle-require', async () => {
6+
// mock importModule from @code-pushup/utils to bypass jiti which doesn't work with memfs
7+
vi.mock('@code-pushup/utils', async () => {
8+
const utils: object = await vi.importActual('@code-pushup/utils');
89
const { CORE_CONFIG_MOCK }: Record<string, CoreConfig> =
910
await vi.importActual('@code-pushup/test-fixtures');
1011

12+
const mockImportModule = async (options: { filepath: string }) => {
13+
const extension = options.filepath.split('.').at(-1);
14+
return {
15+
...CORE_CONFIG_MOCK,
16+
upload: {
17+
...CORE_CONFIG_MOCK?.upload,
18+
project: extension, // returns loaded file extension to check format precedence
19+
},
20+
};
21+
};
22+
1123
return {
12-
bundleRequire: vi
13-
.fn()
14-
.mockImplementation((options: { filepath: string }) => {
15-
const extension = options.filepath.split('.').at(-1);
16-
return {
17-
mod: {
18-
default: {
19-
...CORE_CONFIG_MOCK,
20-
upload: {
21-
...CORE_CONFIG_MOCK?.upload,
22-
project: extension, // returns loaded file extension to check format precedence
23-
},
24-
},
25-
},
26-
};
27-
}),
24+
...utils,
25+
importModule: mockImportModule,
2826
};
2927
});
3028

31-
// Note: memfs files are only listed to satisfy a system check, value is used from bundle-require mock
29+
// Note: memfs files are used for fileExists checks, but the actual import uses the mocked importModule
3230
describe('autoloadRc', () => {
3331
it('prioritise a .ts configuration file', async () => {
3432
vol.fromJSON(

packages/plugin-coverage/src/lib/nx/coverage-paths.unit.test.ts

Lines changed: 68 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,80 +13,86 @@ import {
1313
getCoveragePathsForTarget,
1414
} from './coverage-paths.js';
1515

16-
vi.mock('bundle-require', () => ({
17-
bundleRequire: vi.fn().mockImplementation((options: { filepath: string }) => {
18-
const VITEST_VALID: VitestCoverageConfig = {
19-
test: {
20-
coverage: {
21-
reporter: ['lcov'],
22-
reportsDirectory: path.join('coverage', 'cli'),
23-
},
16+
// mock importModule from @code-pushup/utils to bypass jiti which doesn't work with memfs
17+
vi.mock('@code-pushup/utils', async () => {
18+
const utils: object = await vi.importActual('@code-pushup/utils');
19+
20+
const VITEST_VALID: VitestCoverageConfig = {
21+
test: {
22+
coverage: {
23+
reporter: ['lcov'],
24+
reportsDirectory: path.join('coverage', 'cli'),
2425
},
25-
};
26+
},
27+
};
2628

27-
const VITEST_NO_DIR: VitestCoverageConfig = {
28-
test: { coverage: { reporter: ['lcov'] } },
29-
};
29+
const VITEST_NO_DIR: VitestCoverageConfig = {
30+
test: { coverage: { reporter: ['lcov'] } },
31+
};
3032

31-
const VITEST_NO_LCOV: VitestCoverageConfig = {
32-
test: {
33-
coverage: {
34-
reporter: ['json'],
35-
reportsDirectory: 'coverage',
36-
},
33+
const VITEST_NO_LCOV: VitestCoverageConfig = {
34+
test: {
35+
coverage: {
36+
reporter: ['json'],
37+
reportsDirectory: 'coverage',
3738
},
38-
};
39+
},
40+
};
3941

40-
const JEST_VALID: JestCoverageConfig = {
41-
coverageReporters: ['lcov'],
42-
coverageDirectory: path.join('coverage', 'core'),
43-
};
42+
const JEST_VALID: JestCoverageConfig = {
43+
coverageReporters: ['lcov'],
44+
coverageDirectory: path.join('coverage', 'core'),
45+
};
4446

45-
const JEST_NO_DIR: JestCoverageConfig = {
46-
coverageReporters: ['lcov'],
47-
};
47+
const JEST_NO_DIR: JestCoverageConfig = {
48+
coverageReporters: ['lcov'],
49+
};
4850

49-
const JEST_NO_LCOV: JestCoverageConfig = {
50-
coverageReporters: ['json'],
51-
coverageDirectory: 'coverage',
52-
};
51+
const JEST_NO_LCOV: JestCoverageConfig = {
52+
coverageReporters: ['json'],
53+
coverageDirectory: 'coverage',
54+
};
5355

54-
const JEST_PRESET: JestCoverageConfig & { preset?: string } = {
55-
preset: '../../jest.preset.ts',
56-
coverageDirectory: 'coverage',
57-
};
56+
const JEST_PRESET: JestCoverageConfig & { preset?: string } = {
57+
preset: '../../jest.preset.ts',
58+
coverageDirectory: 'coverage',
59+
};
5860

59-
const wrapReturnValue = (
60-
value: VitestCoverageConfig | JestCoverageConfig,
61-
) => ({ mod: { default: value } });
62-
63-
const config = options.filepath.split('.')[0];
64-
switch (config) {
65-
case 'vitest-valid':
66-
return wrapReturnValue(VITEST_VALID);
67-
case 'vitest-no-lcov':
68-
return wrapReturnValue(VITEST_NO_LCOV);
69-
case 'vitest-no-dir':
70-
return wrapReturnValue(VITEST_NO_DIR);
71-
case 'jest-valid':
72-
return wrapReturnValue(JEST_VALID);
73-
case 'jest-no-lcov':
74-
return wrapReturnValue(JEST_NO_LCOV);
75-
case 'jest-no-dir':
76-
return wrapReturnValue(JEST_NO_DIR);
77-
case 'jest-preset':
78-
return wrapReturnValue(JEST_PRESET);
79-
default:
80-
return wrapReturnValue({});
81-
}
82-
}),
83-
}));
61+
return {
62+
...utils,
63+
importModule: vi
64+
.fn()
65+
.mockImplementation((options: { filepath: string }) => {
66+
// Extract config name from filename (handles both absolute and relative paths)
67+
const filename = path.basename(options.filepath);
68+
const config = filename.split('.')[0];
69+
switch (config) {
70+
case 'vitest-valid':
71+
return Promise.resolve(VITEST_VALID);
72+
case 'vitest-no-lcov':
73+
return Promise.resolve(VITEST_NO_LCOV);
74+
case 'vitest-no-dir':
75+
return Promise.resolve(VITEST_NO_DIR);
76+
case 'jest-valid':
77+
return Promise.resolve(JEST_VALID);
78+
case 'jest-no-lcov':
79+
return Promise.resolve(JEST_NO_LCOV);
80+
case 'jest-no-dir':
81+
return Promise.resolve(JEST_NO_DIR);
82+
case 'jest-preset':
83+
return Promise.resolve(JEST_PRESET);
84+
default:
85+
return Promise.resolve({});
86+
}
87+
}),
88+
};
89+
});
8490

8591
describe('getCoveragePathForTarget', () => {
8692
beforeEach(() => {
8793
vol.fromJSON(
8894
{
89-
// values come from bundle-require mock above
95+
// values come from importModule mock above
9096
'vitest-valid.config.ts': '',
9197
'jest-valid.config.ts': '',
9298
},
@@ -162,7 +168,7 @@ describe('getCoveragePathForVitest', () => {
162168
beforeEach(() => {
163169
vol.fromJSON(
164170
{
165-
// values come from bundle-require mock above
171+
// values come from importModule mock above
166172
'vitest-valid.config.unit.ts': '',
167173
'vitest-no-dir.config.integration.ts': '',
168174
'vitest-no-lcov.config.integration.ts': '',
@@ -260,7 +266,7 @@ describe('getCoveragePathForJest', () => {
260266
beforeEach(() => {
261267
vol.fromJSON(
262268
{
263-
// values come from bundle-require mock above
269+
// values come from importModule mock above
264270
'jest-preset.config.ts': '',
265271
'jest-valid.config.unit.ts': '',
266272
'jest-valid.config.integration.ts': '',

packages/plugin-lighthouse/src/lib/runner/utils.unit.test.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,25 @@ import {
2525
withLocalTmpDir,
2626
} from './utils.js';
2727

28-
// mock bundleRequire inside importEsmModule used for fetching config
29-
vi.mock('bundle-require', async () => {
28+
// mock importModule from @code-pushup/utils to bypass jiti which doesn't work with memfs
29+
vi.mock('@code-pushup/utils', async () => {
30+
const utils: object = await vi.importActual('@code-pushup/utils');
3031
const { CORE_CONFIG_MOCK }: Record<string, CoreConfig> =
3132
await vi.importActual('@code-pushup/test-utils');
3233

3334
return {
34-
bundleRequire: vi
35+
...utils,
36+
importModule: vi
3537
.fn()
3638
.mockImplementation((options: { filepath: string }) => {
3739
const project = options.filepath.split('.').at(-2);
38-
return {
39-
mod: {
40-
default: {
41-
...CORE_CONFIG_MOCK,
42-
upload: {
43-
...CORE_CONFIG_MOCK?.upload,
44-
project, // returns loaded file extension to check in test
45-
},
46-
},
40+
return Promise.resolve({
41+
...CORE_CONFIG_MOCK,
42+
upload: {
43+
...CORE_CONFIG_MOCK?.upload,
44+
project, // returns loaded file extension to check in test
4745
},
48-
};
46+
});
4947
}),
5048
};
5149
});

0 commit comments

Comments
 (0)