|
1 | | -import { join } from 'path'; |
2 | | -import * as process from 'process'; |
| 1 | +import { dirname, join } from 'path'; |
| 2 | +import { fileURLToPath } from 'url'; |
3 | 3 | import { describe, expect } from 'vitest'; |
4 | | -import { persistConfig, uploadConfig } from '@code-pushup/models/testing'; |
5 | 4 | import { readCodePushupConfig } from './read-code-pushup-config'; |
6 | 5 |
|
7 | 6 | describe('readCodePushupConfig', () => { |
8 | | - it('should load file', async () => { |
9 | | - const module = await readCodePushupConfig( |
10 | | - join(process.cwd(), 'packages', 'core', 'test', 'minimal.config.mjs'), |
11 | | - ); |
12 | | - expect(module).toEqual( |
| 7 | + const configDirPath = join( |
| 8 | + fileURLToPath(dirname(import.meta.url)), |
| 9 | + '..', |
| 10 | + '..', |
| 11 | + '..', |
| 12 | + '..', |
| 13 | + '..', |
| 14 | + 'testing-utils', |
| 15 | + 'src', |
| 16 | + 'lib', |
| 17 | + 'fixtures', |
| 18 | + 'configs', |
| 19 | + ); |
| 20 | + |
| 21 | + it('should load a valid configuration file', async () => { |
| 22 | + await expect( |
| 23 | + readCodePushupConfig(join(configDirPath, 'code-pushup.config.ts')), |
| 24 | + ).resolves.toEqual( |
13 | 25 | expect.objectContaining({ |
14 | | - persist: expect.objectContaining(persistConfig()), |
15 | 26 | upload: expect.objectContaining({ |
16 | | - ...uploadConfig(), |
17 | 27 | organization: 'code-pushup', |
18 | 28 | }), |
19 | | - categories: expect.arrayContaining([ |
20 | | - expect.objectContaining({ |
21 | | - slug: 'category-slug-1', |
22 | | - }), |
23 | | - ]), |
| 29 | + categories: expect.any(Array), |
24 | 30 | plugins: expect.arrayContaining([ |
25 | 31 | expect.objectContaining({ |
26 | | - slug: 'mock-plugin-slug', |
| 32 | + slug: 'node', |
27 | 33 | }), |
28 | 34 | ]), |
29 | 35 | }), |
30 | 36 | ); |
31 | 37 | }); |
32 | 38 |
|
33 | | - it('should throw if filepath not given', async () => { |
34 | | - const filepath = ''; |
35 | | - await expect(readCodePushupConfig(filepath)).rejects.toThrow( |
36 | | - `No filepath provided`, |
| 39 | + it('should throw if the path is empty', async () => { |
| 40 | + await expect(readCodePushupConfig('')).rejects.toThrow( |
| 41 | + 'The configuration path is empty.', |
37 | 42 | ); |
38 | 43 | }); |
39 | 44 |
|
40 | | - it('should throw if file does not exist', async () => { |
41 | | - const filepath = join('invalid-path', 'valid-export.mjs'); |
42 | | - await expect(readCodePushupConfig(filepath)).rejects.toThrow( |
43 | | - `invalid-path`, |
44 | | - ); |
| 45 | + it('should throw if the file does not exist', async () => { |
| 46 | + await expect( |
| 47 | + readCodePushupConfig(join('non-existent', 'config.file.js')), |
| 48 | + ).rejects.toThrow(/Provided path .* is not valid./); |
45 | 49 | }); |
46 | 50 |
|
47 | | - it('should throw if config is invalid', async () => { |
48 | | - const filepath = join( |
49 | | - process.cwd(), |
50 | | - 'packages', |
51 | | - 'core', |
52 | | - 'test', |
53 | | - 'invaid.config.mjs', |
54 | | - ); |
55 | | - await expect(readCodePushupConfig(filepath)).rejects.toThrow( |
56 | | - `"code": "invalid_type",`, |
57 | | - ); |
| 51 | + it('should throw if the configuration is empty', async () => { |
| 52 | + await expect( |
| 53 | + readCodePushupConfig(join(configDirPath, 'code-pushup.empty.config.js')), |
| 54 | + ).rejects.toThrow(`"code": "invalid_type",`); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should throw if the configuration is invalid', async () => { |
| 58 | + await expect( |
| 59 | + readCodePushupConfig( |
| 60 | + join(configDirPath, 'code-pushup.invalid.config.ts'), |
| 61 | + ), |
| 62 | + ).rejects.toThrow(/refs are duplicates/); |
58 | 63 | }); |
59 | 64 | }); |
0 commit comments