-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathread-rc-file.unit.test.ts
More file actions
82 lines (73 loc) · 2.39 KB
/
read-rc-file.unit.test.ts
File metadata and controls
82 lines (73 loc) · 2.39 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
import { vol } from 'memfs';
import { CONFIG_FILE_NAME, type CoreConfig } from '@code-pushup/models';
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
import { autoloadRc } from './read-rc-file.js';
// mock importModule from @code-pushup/utils used for fetching config
vi.mock('@code-pushup/utils', async () => {
const utils =
await vi.importActual<typeof import('@code-pushup/utils')>(
'@code-pushup/utils',
);
const { CORE_CONFIG_MOCK }: Record<string, CoreConfig> =
await vi.importActual('@code-pushup/test-fixtures');
return {
...utils,
importModule: vi
.fn()
.mockImplementation(async (options: { filepath: string }) => {
const extension = options.filepath.split('.').at(-1);
return {
...CORE_CONFIG_MOCK,
upload: {
...CORE_CONFIG_MOCK?.upload,
project: extension, // returns loaded file extension to check format precedence
},
};
}),
};
});
// Note: memfs files are only listed to satisfy a system check, value is used from importModule mock
describe('autoloadRc', () => {
it('prioritise a .ts configuration file', async () => {
vol.fromJSON(
{
[`${CONFIG_FILE_NAME}.js`]: '',
[`${CONFIG_FILE_NAME}.mjs`]: '',
[`${CONFIG_FILE_NAME}.ts`]: '',
},
MEMFS_VOLUME,
);
await expect(autoloadRc()).resolves.toEqual(
expect.objectContaining({
upload: expect.objectContaining({ project: 'ts' }),
}),
);
});
it('should prioritise .mjs configuration file over .js', async () => {
vol.fromJSON(
{
[`${CONFIG_FILE_NAME}.js`]: '',
[`${CONFIG_FILE_NAME}.mjs`]: '',
},
MEMFS_VOLUME,
);
await expect(autoloadRc()).resolves.toEqual(
expect.objectContaining({
upload: expect.objectContaining({ project: 'mjs' }),
}),
);
});
it('should load a .js configuration file if no other valid extension exists', async () => {
vol.fromJSON({ [`${CONFIG_FILE_NAME}.js`]: '' }, MEMFS_VOLUME);
await expect(autoloadRc()).resolves.toEqual(
expect.objectContaining({
upload: expect.objectContaining({ project: 'js' }),
}),
);
});
it('should throw if no configuration file is present', async () => {
await expect(autoloadRc()).rejects.toThrow(
`No code-pushup.config.{ts,mjs,js} file present in ${MEMFS_VOLUME}`,
);
});
});