Skip to content

Commit 207bf07

Browse files
authored
chore: clean up core config tests, remove obsolete logic (#435)
- fixed unit tests for `configMiddleware` to check only the immediate underlying logic - `readCodePushUpConfig` function and related tests were removed as they are no longer used (replaced by`readRcByPath`) - polished comments
1 parent 984d7f0 commit 207bf07

6 files changed

Lines changed: 31 additions & 153 deletions

File tree

packages/cli/src/lib/implementation/core-config.options-and-middleware.integration.test.ts renamed to packages/cli/src/lib/implementation/core-config.integration.test.ts

File renamed without changes.
Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,27 @@
11
import { describe, expect, vi } from 'vitest';
2-
import { ConfigPathError } from '@code-pushup/core';
3-
import { CoreConfig } from '@code-pushup/models';
4-
import { CORE_CONFIG_MOCK } from '@code-pushup/testing-utils';
2+
import { autoloadRc, readRcByPath } from '@code-pushup/core';
53
import { coreConfigMiddleware } from './core-config.middleware';
64

75
vi.mock('@code-pushup/core', async () => {
6+
const { CORE_CONFIG_MOCK }: typeof import('@code-pushup/testing-utils') =
7+
await vi.importActual('@code-pushup/testing-utils');
88
const core: object = await vi.importActual('@code-pushup/core');
99
return {
1010
...core,
11-
readRcByPath: vi.fn().mockImplementation((filepath: string): CoreConfig => {
12-
const extension = filepath.split('.');
13-
if (filepath.includes('throw-error')) {
14-
throw new ConfigPathError(filepath);
15-
}
16-
return {
17-
...CORE_CONFIG_MOCK,
18-
upload: {
19-
...CORE_CONFIG_MOCK.upload,
20-
project: `cli-${extension}`,
21-
},
22-
};
23-
}),
24-
autoloadRc: vi.fn().mockImplementation(
25-
(): CoreConfig => ({
26-
...CORE_CONFIG_MOCK,
27-
upload: {
28-
...CORE_CONFIG_MOCK.upload,
29-
project: `cli-autoload`,
30-
},
31-
}),
32-
),
11+
readRcByPath: vi.fn().mockResolvedValue(CORE_CONFIG_MOCK),
12+
autoloadRc: vi.fn().mockResolvedValue(CORE_CONFIG_MOCK),
3313
};
3414
});
3515

3616
describe('coreConfigMiddleware', () => {
37-
it('should load code-pushup.config.(ts|mjs|js) by default', async () => {
38-
const config = await coreConfigMiddleware({});
39-
expect(config?.upload?.project).toBe('cli-autoload');
17+
it('should attempt to load code-pushup.config.(ts|mjs|js) by default', async () => {
18+
await coreConfigMiddleware({});
19+
expect(autoloadRc).toHaveBeenCalled();
4020
});
4121

42-
it.each(['ts', 'mjs', 'js'])(
43-
'should load a valid .%s config',
44-
async extension => {
45-
const config = await coreConfigMiddleware({
46-
config: `code-pushup.config.${extension}`,
47-
});
48-
expect(config.config).toContain(`code-pushup.config.${extension}`);
49-
expect(config.upload?.project).toContain(extension);
50-
},
51-
);
52-
53-
it('should throw with invalid config path', async () => {
54-
await expect(
55-
coreConfigMiddleware({ config: 'throw-error' }),
56-
).rejects.toThrow(/Provided path .* is not valid./);
22+
it('should directly attempt to load passed config', async () => {
23+
await coreConfigMiddleware({ config: 'cli/custom-config.mjs' });
24+
expect(autoloadRc).not.toHaveBeenCalled();
25+
expect(readRcByPath).toHaveBeenCalledWith('cli/custom-config.mjs');
5726
});
5827
});

packages/core/src/index.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
export { readCodePushupConfig } from './lib/implementation/read-code-pushup-config';
21
export {
3-
persistReport,
4-
PersistError,
5-
PersistDirError,
6-
} from './lib/implementation/persist';
2+
CollectAndPersistReportsOptions,
3+
collectAndPersistReports,
4+
} from './lib/collect-and-persist';
5+
export { CollectOptions, collect } from './lib/implementation/collect';
76
export {
7+
PluginOutputMissingAuditError,
88
executePlugin,
99
executePlugins,
10-
PluginOutputMissingAuditError,
1110
} from './lib/implementation/execute-plugin';
12-
export { collect, CollectOptions } from './lib/implementation/collect';
13-
export { upload, UploadOptions } from './lib/upload';
14-
export { GlobalOptions } from './lib/types';
1511
export {
16-
collectAndPersistReports,
17-
CollectAndPersistReportsOptions,
18-
} from './lib/collect-and-persist';
12+
PersistDirError,
13+
PersistError,
14+
persistReport,
15+
} from './lib/implementation/persist';
1916
export {
17+
ConfigPathError,
2018
autoloadRc,
2119
readRcByPath,
22-
ConfigPathError,
2320
} from './lib/implementation/read-rc-file';
21+
export { GlobalOptions } from './lib/types';
22+
export { UploadOptions, upload } from './lib/upload';

packages/core/src/lib/implementation/read-code-pushup-config.integration.test.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

packages/core/src/lib/implementation/read-code-pushup-config.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CONFIG_FILE_NAME, CoreConfig } from '@code-pushup/models';
44
import { MEMFS_VOLUME } from '@code-pushup/testing-utils';
55
import { autoloadRc } from './read-rc-file';
66

7-
// Mock bundleRequire inside importEsmModule used for fetching config
7+
// mock bundleRequire inside importEsmModule used for fetching config
88
vi.mock('bundle-require', async () => {
99
const { CORE_CONFIG_MOCK }: Record<string, CoreConfig> =
1010
await vi.importActual('@code-pushup/testing-utils');
@@ -21,8 +21,7 @@ vi.mock('bundle-require', async () => {
2121
...CORE_CONFIG_MOCK,
2222
upload: {
2323
...CORE_CONFIG_MOCK?.upload,
24-
// this value is mocked from the above mock setup. It is always the extension of the loaded file
25-
project: extension,
24+
project: extension, // returns loaded file extension to check format precedence
2625
},
2726
},
2827
},
@@ -31,11 +30,11 @@ vi.mock('bundle-require', async () => {
3130
};
3231
});
3332

33+
// Note: memfs files are only listed to satisfy a system check, value is used from bundle-require mock
3434
describe('autoloadRc', () => {
3535
it('prioritise a .ts configuration file', async () => {
3636
vol.fromJSON(
3737
{
38-
// this is just here to satisfy the file system check. the file ise served over a mock in bundleRequire
3938
[`${CONFIG_FILE_NAME}.js`]: '',
4039
[`${CONFIG_FILE_NAME}.mjs`]: '',
4140
[`${CONFIG_FILE_NAME}.ts`]: '',
@@ -45,17 +44,14 @@ describe('autoloadRc', () => {
4544

4645
await expect(autoloadRc()).resolves.toEqual(
4746
expect.objectContaining({
48-
upload: expect.objectContaining({
49-
project: 'ts',
50-
}),
47+
upload: expect.objectContaining({ project: 'ts' }),
5148
}),
5249
);
5350
});
5451

5552
it('should prioritise .mjs configuration file over .js', async () => {
5653
vol.fromJSON(
5754
{
58-
// this is just here to satisfy the file system check. the file ise served over a mock in bundleRequire
5955
[`${CONFIG_FILE_NAME}.js`]: '',
6056
[`${CONFIG_FILE_NAME}.mjs`]: '',
6157
},
@@ -64,27 +60,17 @@ describe('autoloadRc', () => {
6460

6561
await expect(autoloadRc()).resolves.toEqual(
6662
expect.objectContaining({
67-
upload: expect.objectContaining({
68-
project: 'mjs',
69-
}),
63+
upload: expect.objectContaining({ project: 'mjs' }),
7064
}),
7165
);
7266
});
7367

7468
it('should load a .js configuration file if no other valid extension exists', async () => {
75-
vol.fromJSON(
76-
{
77-
// this is just here to satisfy the file system check. the file ise served over a mock in bundleRequire
78-
[`${CONFIG_FILE_NAME}.js`]: '',
79-
},
80-
MEMFS_VOLUME,
81-
);
69+
vol.fromJSON({ [`${CONFIG_FILE_NAME}.js`]: '' }, MEMFS_VOLUME);
8270

8371
await expect(autoloadRc()).resolves.toEqual(
8472
expect.objectContaining({
85-
upload: expect.objectContaining({
86-
project: 'js',
87-
}),
73+
upload: expect.objectContaining({ project: 'js' }),
8874
}),
8975
);
9076
});

0 commit comments

Comments
 (0)