|
| 1 | +import type { CoreConfig } from '@code-pushup/models'; |
| 2 | +import type { ConfigPatterns } from './models.js'; |
| 3 | +import { parseConfigPatternsFromString } from './settings.js'; |
| 4 | + |
| 5 | +describe('parseConfigPatternsFromString', () => { |
| 6 | + it('should return for empty string', () => { |
| 7 | + expect(parseConfigPatternsFromString('')).toBeNull(); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should parse full persist and upload configs', () => { |
| 11 | + const configPatterns: Required<ConfigPatterns> = { |
| 12 | + persist: { |
| 13 | + outputDir: '.code-pushup/{projectName}', |
| 14 | + filename: 'report', |
| 15 | + format: ['json', 'md'], |
| 16 | + skipReports: false, |
| 17 | + }, |
| 18 | + upload: { |
| 19 | + server: 'https://api.code-pushup.example.com/graphql', |
| 20 | + apiKey: 'cp_...', |
| 21 | + organization: 'example', |
| 22 | + project: '{projectName}', |
| 23 | + }, |
| 24 | + }; |
| 25 | + expect( |
| 26 | + parseConfigPatternsFromString(JSON.stringify(configPatterns)), |
| 27 | + ).toEqual(configPatterns); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should parse full persist config without upload config', () => { |
| 31 | + const configPatterns: ConfigPatterns = { |
| 32 | + persist: { |
| 33 | + outputDir: '.code-pushup/{projectName}', |
| 34 | + filename: 'report', |
| 35 | + format: ['json', 'md'], |
| 36 | + skipReports: false, |
| 37 | + }, |
| 38 | + }; |
| 39 | + expect( |
| 40 | + parseConfigPatternsFromString(JSON.stringify(configPatterns)), |
| 41 | + ).toEqual(configPatterns); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should fill in default persist values where missing', () => { |
| 45 | + expect( |
| 46 | + parseConfigPatternsFromString( |
| 47 | + JSON.stringify({ |
| 48 | + persist: { |
| 49 | + filename: '{projectName}-report', |
| 50 | + }, |
| 51 | + } satisfies Pick<CoreConfig, 'persist'>), |
| 52 | + ), |
| 53 | + ).toEqual<ConfigPatterns>({ |
| 54 | + persist: { |
| 55 | + outputDir: '.code-pushup', |
| 56 | + filename: '{projectName}-report', |
| 57 | + format: ['json', 'md'], |
| 58 | + skipReports: false, |
| 59 | + }, |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should throw if input string is not valid JSON', () => { |
| 64 | + expect(() => |
| 65 | + parseConfigPatternsFromString('outputDir: .code-pushup/{projectName}'), |
| 66 | + ).toThrow('Invalid JSON value for configPatterns input - Unexpected token'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should throw if persist config is missing', () => { |
| 70 | + expect(() => parseConfigPatternsFromString('{}')).toThrow( |
| 71 | + /Invalid shape of configPatterns input.*expected object, received undefined.*at persist/s, |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should throw if persist config has invalid values', () => { |
| 76 | + expect(() => |
| 77 | + parseConfigPatternsFromString( |
| 78 | + JSON.stringify({ persist: { format: 'json' } }), |
| 79 | + ), |
| 80 | + ).toThrow( |
| 81 | + /Invalid shape of configPatterns input.*expected array, received string.*at persist\.format/s, |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should throw if upload config has missing values', () => { |
| 86 | + expect(() => |
| 87 | + parseConfigPatternsFromString( |
| 88 | + JSON.stringify({ |
| 89 | + persist: {}, |
| 90 | + upload: { |
| 91 | + server: 'https://api.code-pushup.example.com/graphql', |
| 92 | + organization: 'example', |
| 93 | + project: '{projectName}', |
| 94 | + }, |
| 95 | + }), |
| 96 | + ), |
| 97 | + ).toThrow( |
| 98 | + /Invalid shape of configPatterns input.*expected string, received undefined.*at upload\.apiKey/s, |
| 99 | + ); |
| 100 | + }); |
| 101 | +}); |
0 commit comments