1
+ import chalk from 'chalk' ;
1
2
import { vol } from 'memfs' ;
2
- import { describe , expect , it , vi } from 'vitest' ;
3
+ import { describe , expect , it } from 'vitest' ;
3
4
import { AuditOutputs , PluginConfig } from '@code-pushup/models' ;
4
5
import {
5
6
MEMFS_VOLUME ,
6
7
MINIMAL_PLUGIN_CONFIG_MOCK ,
7
- getLogMessages ,
8
8
} from '@code-pushup/test-utils' ;
9
- import { ui } from '@code-pushup/utils' ;
10
9
import {
11
10
PluginOutputMissingAuditError ,
12
11
executePlugin ,
@@ -65,7 +64,7 @@ describe('executePlugin', () => {
65
64
] ) ;
66
65
} ) ;
67
66
68
- it ( 'should throw when plugin slug is invalid' , async ( ) => {
67
+ it ( 'should throw when audit slug is invalid' , async ( ) => {
69
68
await expect ( ( ) =>
70
69
executePlugin ( {
71
70
...MINIMAL_PLUGIN_CONFIG_MOCK ,
@@ -74,19 +73,24 @@ describe('executePlugin', () => {
74
73
) . rejects . toThrow ( new PluginOutputMissingAuditError ( 'node-version' ) ) ;
75
74
} ) ;
76
75
77
- it ( 'should throw if invalid runnerOutput is produced' , async ( ) => {
76
+ it ( 'should throw for missing audit' , async ( ) => {
77
+ const missingSlug = 'missing-audit-slug' ;
78
78
await expect ( ( ) =>
79
79
executePlugin ( {
80
80
...MINIMAL_PLUGIN_CONFIG_MOCK ,
81
81
runner : ( ) => [
82
82
{
83
- slug : '-invalid-audit-slug' ,
83
+ slug : missingSlug ,
84
84
score : 0 ,
85
85
value : 0 ,
86
86
} ,
87
87
] ,
88
88
} ) ,
89
- ) . rejects . toThrow ( 'The slug has to follow the pattern' ) ;
89
+ ) . rejects . toThrow (
90
+ `Audit metadata not present in plugin config. Missing slug: ${ chalk . bold (
91
+ missingSlug ,
92
+ ) } `,
93
+ ) ;
90
94
} ) ;
91
95
} ) ;
92
96
@@ -108,55 +112,153 @@ describe('executePlugins', () => {
108
112
expect ( pluginResult [ 0 ] ?. audits [ 0 ] ?. slug ) . toBe ( 'node-version' ) ;
109
113
} ) ;
110
114
111
- it ( 'should throw for invalid plugins' , async ( ) => {
115
+ it ( 'should throw for invalid audit output' , async ( ) => {
116
+ const slug = 'simulate-invalid-audit-slug' ;
117
+ const title = 'Simulate an invalid audit slug in outputs' ;
112
118
await expect ( ( ) =>
113
119
executePlugins (
114
120
[
115
- MINIMAL_PLUGIN_CONFIG_MOCK ,
116
121
{
117
122
...MINIMAL_PLUGIN_CONFIG_MOCK ,
118
- audits : [ { slug : '-invalid-slug' , title : 'Invalid audit' } ] ,
123
+ slug,
124
+ title,
125
+ runner : ( ) => [
126
+ {
127
+ slug : 'invalid-audit-slug-' ,
128
+ score : 0.3 ,
129
+ value : 16 ,
130
+ displayValue : '16.0.0' ,
131
+ } ,
132
+ ] ,
119
133
} ,
120
134
] satisfies PluginConfig [ ] ,
121
135
{ progress : false } ,
122
136
) ,
123
- ) . rejects . toThrow (
124
- / P l u g i n s f a i l e d : 1 e r r o r s : .* A u d i t m e t a d a t a n o t f o u n d f o r s l u g n o d e - v e r s i o n / ,
125
- ) ;
137
+ ) . rejects
138
+ . toThrow ( `Executing 1 plugin failed.\n\nError: - Plugin ${ chalk . bold (
139
+ title ,
140
+ ) } (${ chalk . bold ( slug ) } ) produced the following error:
141
+ - Audit output is invalid: [
142
+ {
143
+ "validation": "regex",
144
+ "code": "invalid_string",
145
+ "message": "The slug has to follow the pattern [0-9a-z] followed by multiple optional groups of -[0-9a-z]. e.g. my-slug",
146
+ "path": [
147
+ 0,
148
+ "slug"
149
+ ]
150
+ }
151
+ ]
152
+ ` ) ;
126
153
} ) ;
127
154
128
- it ( 'should print invalid plugin errors and throw' , async ( ) => {
129
- const pluginConfig = {
130
- ...MINIMAL_PLUGIN_CONFIG_MOCK ,
131
- runner : vi
132
- . fn ( )
133
- . mockRejectedValue ( 'Audit metadata not found for slug node-version' ) ,
134
- } ;
135
- const pluginConfig2 = {
136
- ...MINIMAL_PLUGIN_CONFIG_MOCK ,
137
- runner : vi . fn ( ) . mockResolvedValue ( [ ] ) ,
138
- } ;
139
- const pluginConfig3 = {
140
- ...MINIMAL_PLUGIN_CONFIG_MOCK ,
141
- runner : vi . fn ( ) . mockRejectedValue ( 'plugin 3 error' ) ,
142
- } ;
155
+ it ( 'should throw for one failing plugin' , async ( ) => {
156
+ const missingAuditSlug = 'missing-audit-slug' ;
157
+ await expect ( ( ) =>
158
+ executePlugins (
159
+ [
160
+ {
161
+ ...MINIMAL_PLUGIN_CONFIG_MOCK ,
162
+ slug : 'plg1' ,
163
+ title : 'plg1' ,
164
+ runner : ( ) => [
165
+ {
166
+ slug : `${ missingAuditSlug } -a` ,
167
+ score : 0.3 ,
168
+ value : 16 ,
169
+ displayValue : '16.0.0' ,
170
+ } ,
171
+ ] ,
172
+ } ,
173
+ ] satisfies PluginConfig [ ] ,
174
+ { progress : false } ,
175
+ ) ,
176
+ ) . rejects . toThrow ( 'Executing 1 plugin failed.\n\n' ) ;
177
+ } ) ;
143
178
179
+ it ( 'should throw for multiple failing plugins' , async ( ) => {
180
+ const missingAuditSlug = 'missing-audit-slug' ;
144
181
await expect ( ( ) =>
145
- executePlugins ( [ pluginConfig , pluginConfig2 , pluginConfig3 ] , {
146
- progress : false ,
147
- } ) ,
182
+ executePlugins (
183
+ [
184
+ {
185
+ ...MINIMAL_PLUGIN_CONFIG_MOCK ,
186
+ slug : 'plg1' ,
187
+ title : 'plg1' ,
188
+ runner : ( ) => [
189
+ {
190
+ slug : `${ missingAuditSlug } -a` ,
191
+ score : 0.3 ,
192
+ value : 16 ,
193
+ displayValue : '16.0.0' ,
194
+ } ,
195
+ ] ,
196
+ } ,
197
+ {
198
+ ...MINIMAL_PLUGIN_CONFIG_MOCK ,
199
+ slug : 'plg2' ,
200
+ title : 'plg2' ,
201
+ runner : ( ) => [
202
+ {
203
+ slug : `${ missingAuditSlug } -b` ,
204
+ score : 0.3 ,
205
+ value : 16 ,
206
+ displayValue : '16.0.0' ,
207
+ } ,
208
+ ] ,
209
+ } ,
210
+ ] satisfies PluginConfig [ ] ,
211
+ { progress : false } ,
212
+ ) ,
213
+ ) . rejects . toThrow ( 'Executing 2 plugins failed.\n\n' ) ;
214
+ } ) ;
215
+
216
+ it ( 'should throw with indentation in message' , async ( ) => {
217
+ const missingAuditSlug = 'missing-audit-slug' ;
218
+
219
+ await expect ( ( ) =>
220
+ executePlugins (
221
+ [
222
+ {
223
+ ...MINIMAL_PLUGIN_CONFIG_MOCK ,
224
+ slug : 'plg1' ,
225
+ title : 'plg1' ,
226
+ runner : ( ) => [
227
+ {
228
+ slug : `${ missingAuditSlug } -a` ,
229
+ score : 0.3 ,
230
+ value : 16 ,
231
+ displayValue : '16.0.0' ,
232
+ } ,
233
+ ] ,
234
+ } ,
235
+ {
236
+ ...MINIMAL_PLUGIN_CONFIG_MOCK ,
237
+ slug : 'plg2' ,
238
+ title : 'plg2' ,
239
+ runner : ( ) => [
240
+ {
241
+ slug : `${ missingAuditSlug } -b` ,
242
+ score : 0.3 ,
243
+ value : 16 ,
244
+ displayValue : '16.0.0' ,
245
+ } ,
246
+ ] ,
247
+ } ,
248
+ ] satisfies PluginConfig [ ] ,
249
+ { progress : false } ,
250
+ ) ,
148
251
) . rejects . toThrow (
149
- 'Plugins failed: 2 errors: Audit metadata not found for slug node-version, plugin 3 error' ,
252
+ `Error: - Plugin ${ chalk . bold ( 'plg1' ) } (${ chalk . bold (
253
+ 'plg1' ,
254
+ ) } ) produced the following error:\n - Audit metadata not present in plugin config. Missing slug: ${ chalk . bold (
255
+ 'missing-audit-slug-a' ,
256
+ ) } \nError: - Plugin ${ chalk . bold ( 'plg2' ) } (${ chalk . bold (
257
+ 'plg2' ,
258
+ ) } ) produced the following error:\n - Audit metadata not present in plugin config. Missing slug: ${ chalk . bold (
259
+ 'missing-audit-slug-b' ,
260
+ ) } `,
150
261
) ;
151
- const logs = getLogMessages ( ui ( ) . logger ) ;
152
- expect ( logs [ 0 ] ) . toBe ( '[ yellow(warn) ] Plugins failed: ' ) ;
153
- expect ( logs [ 1 ] ) . toBe (
154
- '[ yellow(warn) ] Audit metadata not found for slug node-version' ,
155
- ) ;
156
-
157
- expect ( pluginConfig . runner ) . toHaveBeenCalled ( ) ;
158
- expect ( pluginConfig2 . runner ) . toHaveBeenCalled ( ) ;
159
- expect ( pluginConfig3 . runner ) . toHaveBeenCalled ( ) ;
160
262
} ) ;
161
263
162
264
it ( 'should use outputTransform if provided' , async ( ) => {
0 commit comments