-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathschemas.unit.test.ts
More file actions
107 lines (97 loc) · 3 KB
/
Copy pathschemas.unit.test.ts
File metadata and controls
107 lines (97 loc) · 3 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { ZodError } from 'zod';
import type { ConfigPatterns } from './models.js';
import { configPatternsSchema, interpolatedSlugSchema } from './schemas.js';
describe('interpolatedSlugSchema', () => {
it('should accept a valid slug', () => {
expect(interpolatedSlugSchema.parse('valid-slug')).toBe('valid-slug');
});
it('should accept a slug with {projectName} interpolation', () => {
expect(interpolatedSlugSchema.parse('{projectName}-slug')).toBe(
'{projectName}-slug',
);
});
it('should reject an invalid slug that cannot be fixed by interpolation', () => {
expect(() => interpolatedSlugSchema.parse('Invalid Slug!')).toThrow(
ZodError,
);
});
it('should reject a non-string value', () => {
expect(() => interpolatedSlugSchema.parse(123)).toThrow(ZodError);
});
});
describe('configPatternsSchema', () => {
it('should accept valid persist and upload configs', () => {
const configPatterns: Required<ConfigPatterns> = {
persist: {
outputDir: '.code-pushup/{projectName}',
filename: 'report',
format: ['json', 'md'],
skipReports: false,
},
upload: {
server: 'https://api.code-pushup.example.com/graphql',
apiKey: 'cp_...',
organization: 'example',
project: '{projectName}',
},
};
expect(configPatternsSchema.parse(configPatterns)).toEqual(configPatterns);
});
it('should accept persist config without upload', () => {
const configPatterns: ConfigPatterns = {
persist: {
outputDir: '.code-pushup/{projectName}',
filename: 'report',
format: ['json', 'md'],
skipReports: false,
},
};
expect(configPatternsSchema.parse(configPatterns)).toEqual(configPatterns);
});
it('fills in default persist values if missing', () => {
expect(
configPatternsSchema.parse({
persist: {
filename: '{projectName}-report',
},
}),
).toEqual<ConfigPatterns>({
persist: {
outputDir: '.code-pushup',
filename: '{projectName}-report',
format: ['json', 'md'],
skipReports: false,
},
});
});
it('should reject if persist is missing', () => {
expect(() => configPatternsSchema.parse({})).toThrow(ZodError);
});
it('should reject if persist has invalid values', () => {
expect(() =>
configPatternsSchema.parse({
persist: {
format: 'json', // should be array
},
}),
).toThrow(ZodError);
});
it('should reject if upload is missing required fields', () => {
expect(() =>
configPatternsSchema.parse({
persist: {
outputDir: '.code-pushup/{projectName}',
filename: 'report',
format: ['json', 'md'],
skipReports: false,
},
upload: {
server: 'https://api.code-pushup.example.com/graphql',
organization: 'example',
project: '{projectName}',
// missing apiKey
},
}),
).toThrow(ZodError);
});
});