1+ import type { CategoryConfig } from '@code-pushup/models' ;
12import {
23 computeRelativePresetImport ,
34 generateConfigSource ,
@@ -16,6 +17,24 @@ const ESLINT_PLUGIN: PluginCodegenResult = {
1617 pluginInit : "await eslintPlugin({ patterns: '.' })" ,
1718} ;
1819
20+ const ESLINT_CATEGORIES : CategoryConfig [ ] = [
21+ {
22+ slug : 'bug-prevention' ,
23+ title : 'Bug prevention' ,
24+ refs : [ { type : 'group' , plugin : 'eslint' , slug : 'problems' , weight : 1 } ] ,
25+ } ,
26+ {
27+ slug : 'code-style' ,
28+ title : 'Code style' ,
29+ refs : [ { type : 'group' , plugin : 'eslint' , slug : 'suggestions' , weight : 1 } ] ,
30+ } ,
31+ ] ;
32+
33+ const ESLINT_PLUGIN_WITH_CATEGORIES : PluginCodegenResult = {
34+ ...ESLINT_PLUGIN ,
35+ categories : ESLINT_CATEGORIES ,
36+ } ;
37+
1938describe ( 'generateConfigSource' , ( ) => {
2039 describe ( 'TypeScript format' , ( ) => {
2140 it ( 'should generate config with TODO placeholder when no plugins provided' , ( ) => {
@@ -201,6 +220,86 @@ describe('generateConfigSource', () => {
201220 ) ;
202221 } ) ;
203222 } ) ;
223+
224+ describe ( 'categories' , ( ) => {
225+ it ( 'should include categories block when plugin provides categories' , ( ) => {
226+ expect ( generateConfigSource ( [ ESLINT_PLUGIN_WITH_CATEGORIES ] , 'ts' ) )
227+ . toMatchInlineSnapshot ( `
228+ "import eslintPlugin from '@code-pushup/eslint-plugin';
229+ import type { CoreConfig } from '@code-pushup/models';
230+
231+ export default {
232+ plugins: [
233+ await eslintPlugin({ patterns: '.' }),
234+ ],
235+ categories: [
236+ {
237+ slug: 'bug-prevention',
238+ title: 'Bug prevention',
239+ refs: [
240+ { type: 'group', plugin: 'eslint', slug: 'problems', weight: 1 },
241+ ],
242+ },
243+ {
244+ slug: 'code-style',
245+ title: 'Code style',
246+ refs: [
247+ { type: 'group', plugin: 'eslint', slug: 'suggestions', weight: 1 },
248+ ],
249+ },
250+ ],
251+ } satisfies CoreConfig;
252+ "
253+ ` ) ;
254+ } ) ;
255+
256+ it ( 'should omit categories block when no categories provided' , ( ) => {
257+ const source = generateConfigSource ( [ ESLINT_PLUGIN ] , 'ts' ) ;
258+ expect ( source ) . not . toContain ( 'categories' ) ;
259+ } ) ;
260+
261+ it ( 'should merge categories from multiple plugins' , ( ) => {
262+ const coveragePlugin : PluginCodegenResult = {
263+ imports : [
264+ {
265+ moduleSpecifier : '@code-pushup/coverage-plugin' ,
266+ defaultImport : 'coveragePlugin' ,
267+ } ,
268+ ] ,
269+ pluginInit : 'await coveragePlugin()' ,
270+ categories : [
271+ {
272+ slug : 'code-coverage' ,
273+ title : 'Code coverage' ,
274+ refs : [
275+ {
276+ type : 'group' ,
277+ plugin : 'coverage' ,
278+ slug : 'coverage' ,
279+ weight : 1 ,
280+ } ,
281+ ] ,
282+ } ,
283+ ] ,
284+ } ;
285+ const source = generateConfigSource (
286+ [ ESLINT_PLUGIN_WITH_CATEGORIES , coveragePlugin ] ,
287+ 'ts' ,
288+ ) ;
289+ expect ( source ) . toContain ( "slug: 'bug-prevention'" ) ;
290+ expect ( source ) . toContain ( "slug: 'code-style'" ) ;
291+ expect ( source ) . toContain ( "slug: 'code-coverage'" ) ;
292+ } ) ;
293+
294+ it ( 'should include categories in JS format config' , ( ) => {
295+ const source = generateConfigSource (
296+ [ ESLINT_PLUGIN_WITH_CATEGORIES ] ,
297+ 'js' ,
298+ ) ;
299+ expect ( source ) . toContain ( 'categories: [' ) ;
300+ expect ( source ) . toContain ( "slug: 'bug-prevention'" ) ;
301+ } ) ;
302+ } ) ;
204303} ) ;
205304
206305describe ( 'generatePresetSource' , ( ) => {
@@ -243,6 +342,43 @@ describe('generatePresetSource', () => {
243342 "
244343 ` ) ;
245344 } ) ;
345+
346+ it ( 'should include categories in TS preset source' , ( ) => {
347+ expect ( generatePresetSource ( [ ESLINT_PLUGIN_WITH_CATEGORIES ] , 'ts' ) )
348+ . toMatchInlineSnapshot ( `
349+ "import eslintPlugin from '@code-pushup/eslint-plugin';
350+ import type { CoreConfig } from '@code-pushup/models';
351+
352+ /**
353+ * Creates a Code PushUp config for a project.
354+ * @param project Project name
355+ */
356+ export async function createConfig(project: string): Promise<CoreConfig> {
357+ return {
358+ plugins: [
359+ await eslintPlugin({ patterns: '.' }),
360+ ],
361+ categories: [
362+ {
363+ slug: 'bug-prevention',
364+ title: 'Bug prevention',
365+ refs: [
366+ { type: 'group', plugin: 'eslint', slug: 'problems', weight: 1 },
367+ ],
368+ },
369+ {
370+ slug: 'code-style',
371+ title: 'Code style',
372+ refs: [
373+ { type: 'group', plugin: 'eslint', slug: 'suggestions', weight: 1 },
374+ ],
375+ },
376+ ],
377+ };
378+ }
379+ "
380+ ` ) ;
381+ } ) ;
246382} ) ;
247383
248384describe ( 'generateProjectSource' , ( ) => {
0 commit comments