|
1 | 1 | import { describe, expect, it } from 'vitest';
|
2 |
| -import { config, pluginConfig } from '../../test'; |
3 |
| -import { AuditOutputs } from './audit-output'; |
4 |
| -import { pluginConfigSchema } from './plugin-config'; |
| 2 | +import { PluginConfig, pluginConfigSchema } from './plugin-config'; |
5 | 3 |
|
6 | 4 | describe('pluginConfigSchema', () => {
|
7 |
| - it('should parse if plugin configuration is valid', () => { |
8 |
| - const pluginConfig = config().plugins[0]; |
9 |
| - expect(() => pluginConfigSchema.parse(pluginConfig)).not.toThrow(); |
| 5 | + it('should accept a valid plugin configuration with all entities', () => { |
| 6 | + expect(() => |
| 7 | + pluginConfigSchema.parse({ |
| 8 | + slug: 'eslint', |
| 9 | + title: 'ESLint plugin', |
| 10 | + description: 'This plugin checks ESLint in configured files.', |
| 11 | + docsUrl: 'https://eslint.org/', |
| 12 | + icon: 'eslint', |
| 13 | + runner: { command: 'node', outputFile: 'output.json' }, |
| 14 | + audits: [ |
| 15 | + { slug: 'no-magic-numbers', title: 'Use defined constants' }, |
| 16 | + { slug: 'require-await', title: 'Every async has await.' }, |
| 17 | + ], |
| 18 | + groups: [ |
| 19 | + { |
| 20 | + slug: 'typescript-eslint', |
| 21 | + title: 'TypeScript ESLint rules', |
| 22 | + refs: [{ slug: 'require-await', weight: 2 }], |
| 23 | + }, |
| 24 | + ], |
| 25 | + packageName: 'cli', |
| 26 | + version: 'v0.5.2', |
| 27 | + } satisfies PluginConfig), |
| 28 | + ).not.toThrow(); |
10 | 29 | });
|
11 | 30 |
|
12 |
| - it('should throw if plugin slug has a invalid pattern', () => { |
13 |
| - const invalidPluginSlug = '-invalid-plugin-slug'; |
14 |
| - const pluginConfig = config().plugins[0]; |
15 |
| - pluginConfig.slug = invalidPluginSlug; |
16 |
| - |
17 |
| - expect(() => pluginConfigSchema.parse(pluginConfig)).toThrow( |
18 |
| - `slug has to follow the pattern`, |
19 |
| - ); |
| 31 | + it('should accept a minimal plugin configuration', () => { |
| 32 | + expect(() => |
| 33 | + pluginConfigSchema.parse({ |
| 34 | + slug: 'cypress', |
| 35 | + title: 'Cypress testing', |
| 36 | + icon: 'cypress', |
| 37 | + runner: { command: 'npx cypress run', outputFile: 'e2e-output.json' }, |
| 38 | + audits: [{ slug: 'cypress-e2e', title: 'Cypress E2E results' }], |
| 39 | + } satisfies PluginConfig), |
| 40 | + ).not.toThrow(); |
20 | 41 | });
|
21 | 42 |
|
22 |
| - it('should throw if plugin audits contain invalid slugs', () => { |
23 |
| - const invalidAuditRef = '-invalid-audit-slug'; |
24 |
| - const pluginConfig = config().plugins[0]; |
25 |
| - pluginConfig.audits[0].slug = invalidAuditRef; |
26 |
| - |
27 |
| - expect(() => pluginConfigSchema.parse(pluginConfig)).toThrow( |
28 |
| - `slug has to follow the patter`, |
29 |
| - ); |
| 43 | + it('should accept a plugin configuration with no audits or groups', () => { |
| 44 | + expect(() => |
| 45 | + pluginConfigSchema.parse({ |
| 46 | + slug: 'jest', |
| 47 | + title: 'Jest', |
| 48 | + icon: 'jest', |
| 49 | + runner: { command: 'npm run test', outputFile: 'jest-output.json' }, |
| 50 | + audits: [], |
| 51 | + groups: [], |
| 52 | + } satisfies PluginConfig), |
| 53 | + ).not.toThrow(); |
30 | 54 | });
|
31 | 55 |
|
32 |
| - it('should throw if plugin audits slugs are duplicates', () => { |
33 |
| - const pluginConfig = config().plugins[0]; |
34 |
| - pluginConfig.audits = [...pluginConfig.audits, pluginConfig.audits[0]]; |
35 |
| - |
36 |
| - expect(() => pluginConfigSchema.parse(pluginConfig)).toThrow( |
37 |
| - `In plugin audits the slugs are not unique`, |
| 56 | + it('should throw for a configuration with a group reference missing among audits', () => { |
| 57 | + expect(() => |
| 58 | + pluginConfigSchema.parse({ |
| 59 | + slug: 'cypress', |
| 60 | + title: 'Cypress testing', |
| 61 | + icon: 'cypress', |
| 62 | + runner: { command: 'npx cypress run', outputFile: 'output.json' }, |
| 63 | + audits: [{ slug: 'jest', title: 'Jest' }], |
| 64 | + groups: [ |
| 65 | + { |
| 66 | + slug: 'cyct', |
| 67 | + title: 'Cypress component testing', |
| 68 | + refs: [{ slug: 'cyct', weight: 5 }], |
| 69 | + }, |
| 70 | + ], |
| 71 | + } satisfies PluginConfig), |
| 72 | + ).toThrow( |
| 73 | + "audit ref's needs to point to a audit in this plugin config: cyct", |
38 | 74 | );
|
39 | 75 | });
|
40 | 76 |
|
41 |
| - it('should throw if plugin groups contain invalid slugs', () => { |
42 |
| - const invalidGroupSlug = '-invalid-group-slug'; |
43 |
| - const pluginConfig = config().plugins[1]; |
44 |
| - const groups = pluginConfig.groups!; |
45 |
| - groups[0].slug = invalidGroupSlug; |
46 |
| - pluginConfig.groups = groups; |
47 |
| - |
48 |
| - expect(() => pluginConfigSchema.parse(pluginConfig)).toThrow( |
49 |
| - `slug has to follow the patter`, |
| 77 | + it('should throw for a plugin configuration that has a group but empty audits', () => { |
| 78 | + expect(() => |
| 79 | + pluginConfigSchema.parse({ |
| 80 | + slug: 'cypress', |
| 81 | + title: 'Cypress testing', |
| 82 | + icon: 'cypress', |
| 83 | + runner: { command: 'npx cypress run', outputFile: 'output.json' }, |
| 84 | + audits: [], |
| 85 | + groups: [ |
| 86 | + { |
| 87 | + slug: 'cyct', |
| 88 | + title: 'Cypress component testing', |
| 89 | + refs: [{ slug: 'cyct', weight: 5 }], |
| 90 | + }, |
| 91 | + ], |
| 92 | + } satisfies PluginConfig), |
| 93 | + ).toThrow( |
| 94 | + "audit ref's needs to point to a audit in this plugin config: cyct", |
50 | 95 | );
|
51 | 96 | });
|
52 | 97 |
|
53 |
| - it('should throw if plugin groups have duplicate slugs', () => { |
54 |
| - const pluginConfig = config().plugins[1]; |
55 |
| - const groups = pluginConfig.groups!; |
56 |
| - pluginConfig.groups = [...groups, groups[0]]; |
57 |
| - expect(() => pluginConfigSchema.parse(pluginConfig)).toThrow( |
58 |
| - 'In groups the slugs are not unique', |
59 |
| - ); |
60 |
| - }); |
61 |
| - |
62 |
| - it('should throw if plugin groups refs contain invalid slugs', () => { |
63 |
| - const invalidAuditRef = '-invalid-audit-ref'; |
64 |
| - const pluginConfig = config().plugins[1]; |
65 |
| - const groups = pluginConfig.groups!; |
66 |
| - |
67 |
| - groups[0].refs[0].slug = invalidAuditRef; |
68 |
| - pluginConfig.groups = groups; |
69 |
| - |
70 |
| - expect(() => pluginConfigSchema.parse(pluginConfig)).toThrow( |
71 |
| - `slug has to follow the pattern`, |
72 |
| - ); |
73 |
| - }); |
74 |
| - |
75 |
| - it('should take a outputTransform function', () => { |
76 |
| - const undefinedPluginOutput = [ |
77 |
| - { slug: 'audit-1', errors: 0 }, |
78 |
| - { slug: 'audit-2', errors: 5 }, |
79 |
| - ]; |
80 |
| - const pluginCfg = pluginConfig([]); |
81 |
| - pluginCfg.runner.outputTransform = (data: unknown): AuditOutputs => { |
82 |
| - return (data as typeof undefinedPluginOutput).map(data => ({ |
83 |
| - slug: data.slug, |
84 |
| - score: Number(data.errors === 0), |
85 |
| - value: data.errors, |
86 |
| - })); |
87 |
| - }; |
88 |
| - |
89 |
| - expect( |
90 |
| - pluginConfigSchema.parse(pluginCfg).runner.outputTransform, |
91 |
| - ).toBeDefined(); |
92 |
| - expect( |
93 |
| - pluginConfigSchema.parse(pluginCfg).runner.outputTransform!( |
94 |
| - undefinedPluginOutput, |
95 |
| - ), |
96 |
| - ).toEqual([ |
97 |
| - { slug: 'audit-1', score: 1, value: 0 }, |
98 |
| - { slug: 'audit-2', score: 0, value: 5 }, |
99 |
| - ]); |
| 98 | + it('should throw for an invalid plugin slug', () => { |
| 99 | + expect(() => |
| 100 | + pluginConfigSchema.parse({ |
| 101 | + slug: '-invalid-jest', |
| 102 | + title: 'Jest', |
| 103 | + icon: 'jest', |
| 104 | + runner: { command: 'npm run test', outputFile: 'jest-output.json' }, |
| 105 | + audits: [], |
| 106 | + } satisfies PluginConfig), |
| 107 | + ).toThrow('slug has to follow the pattern'); |
100 | 108 | });
|
101 | 109 | });
|
0 commit comments