-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathplugin-config.unit.test.ts
More file actions
192 lines (177 loc) · 5.62 KB
/
plugin-config.unit.test.ts
File metadata and controls
192 lines (177 loc) · 5.62 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { describe, expect, it } from 'vitest';
import { ZodError } from 'zod';
import {
type PluginConfig,
pluginConfigSchema,
pluginUrlsSchema,
} from './plugin-config.js';
describe('pluginConfigSchema', () => {
it('should accept a valid plugin configuration with all entities', () => {
expect(() =>
pluginConfigSchema.parse({
slug: 'eslint',
title: 'ESLint plugin',
description: 'This plugin checks ESLint in configured files.',
docsUrl: 'https://eslint.org/',
icon: 'eslint',
runner: { command: 'node', outputFile: 'output.json' },
audits: [
{ slug: 'no-magic-numbers', title: 'Use defined constants' },
{ slug: 'require-await', title: 'Every async has await.' },
],
groups: [
{
slug: 'typescript-eslint',
title: 'TypeScript ESLint rules',
refs: [{ slug: 'require-await', weight: 2 }],
},
],
packageName: 'cli',
version: 'v0.5.2',
} satisfies PluginConfig),
).not.toThrow();
});
it('should accept a minimal plugin configuration', () => {
expect(() =>
pluginConfigSchema.parse({
slug: 'cypress',
title: 'Cypress testing',
icon: 'cypress',
runner: { command: 'npx cypress run', outputFile: 'e2e-output.json' },
audits: [{ slug: 'cypress-e2e', title: 'Cypress E2E results' }],
} satisfies PluginConfig),
).not.toThrow();
});
it('should accept a valid plugin configuration with score targets', () => {
expect(() =>
pluginConfigSchema.parse({
slug: 'lighthouse',
title: 'Lighthouse',
icon: 'lighthouse',
runner: async () => [
{
slug: 'first-contentful-paint',
score: 0.28,
value: 3752,
displayValue: '3.8 s',
},
{
slug: 'total-blocking-time',
score: 0.91,
value: 183.5,
displayValue: '180 ms',
},
],
scoreTargets: { 'total-blocking-time': 0.9 },
audits: [
{ slug: 'first-contentful-paint', title: 'First Contentful Paint' },
{ slug: 'total-blocking-time', title: 'Total Blocking Time' },
],
} satisfies PluginConfig),
).not.toThrow();
});
it('should throw for a plugin configuration without audits', () => {
expect(() =>
pluginConfigSchema.parse({
slug: 'jest',
title: 'Jest',
icon: 'jest',
runner: { command: 'npm run test', outputFile: 'jest-output.json' },
audits: [],
} satisfies PluginConfig),
).toThrow('too_small');
});
it('should throw for a configuration with a group reference missing among audits', () => {
expect(() =>
pluginConfigSchema.parse({
slug: 'cypress',
title: 'Cypress testing',
icon: 'cypress',
runner: { command: 'npx cypress run', outputFile: 'output.json' },
audits: [{ slug: 'jest', title: 'Jest' }],
groups: [
{
slug: 'cyct',
title: 'Cypress component testing',
refs: [{ slug: 'cyct', weight: 5 }],
},
],
} satisfies PluginConfig),
).toThrow(
String.raw`Group references audits which don't exist in this plugin: \"cyct\"`,
);
});
it('should throw for a plugin configuration that has a group but empty audits', () => {
expect(() =>
pluginConfigSchema.parse({
slug: 'cypress',
title: 'Cypress testing',
icon: 'cypress',
runner: { command: 'npx cypress run', outputFile: 'output.json' },
audits: [],
groups: [
{
slug: 'cyct',
title: 'Cypress component testing',
refs: [{ slug: 'cyct', weight: 5 }],
},
],
} satisfies PluginConfig),
).toThrow(
String.raw`Group references audits which don't exist in this plugin: \"cyct\"`,
);
});
it('should throw for an invalid plugin slug', () => {
expect(() =>
pluginConfigSchema.parse({
slug: '-invalid-jest',
title: 'Jest',
icon: 'jest',
runner: { command: 'npm run test', outputFile: 'jest-output.json' },
audits: [],
} satisfies PluginConfig),
).toThrow('slug has to follow the pattern');
});
});
describe('pluginUrlsSchema', () => {
it('should accept a single URL string', () => {
expect(() => pluginUrlsSchema.parse('https://example.com')).not.toThrow();
});
it('should accept an array of URLs', () => {
expect(() =>
pluginUrlsSchema.parse([
'https://example.com',
'https://example.com/about',
]),
).not.toThrow();
});
it('should accept a weighted object of URLs', () => {
expect(() =>
pluginUrlsSchema.parse({
'https://example.com': 2,
'https://example.com/about': 1,
}),
).not.toThrow();
});
it('should throw for invalid URL', () => {
expect(() => pluginUrlsSchema.parse('invalid')).toThrow(ZodError);
});
it('should throw for array with invalid URL', () => {
expect(() =>
pluginUrlsSchema.parse(['https://example.com', 'invalid']),
).toThrow(ZodError);
});
it('should throw for object with invalid URL', () => {
expect(() => pluginUrlsSchema.parse({ invalid: 1 })).toThrow(ZodError);
});
it('should throw for invalid negative weight', () => {
expect(() => pluginUrlsSchema.parse({ 'https://example.com': -1 })).toThrow(
ZodError,
);
});
it('should throw for invalid string weight', () => {
expect(() =>
pluginUrlsSchema.parse({ 'https://example.com': '1' }),
).toThrow(ZodError);
});
});