Skip to content

Commit d311b95

Browse files
committed
fix(models): fix use case for a group reference but empty audits, clean up tests
1 parent 1af6059 commit d311b95

File tree

3 files changed

+102
-93
lines changed

3 files changed

+102
-93
lines changed

packages/models/src/lib/implementation/utils.unit.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ describe('hasMissingStrings', () => {
8282
expect(hasMissingStrings(['b'], ['a', 'b'])).toBe(false);
8383
});
8484

85+
it('should return false for two empty arrays', () => {
86+
expect(hasMissingStrings([], [])).toBe(false);
87+
});
88+
8589
it('should return a list of strings from source that are missing in target', () => {
8690
expect(hasMissingStrings(['a', 'b'], ['a', 'c'])).toEqual(['b']);
8791
});

packages/models/src/lib/plugin-config.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,10 @@ function missingRefsFromGroupsErrorMsg(pluginCfg: PluginData) {
5757
}
5858

5959
function getMissingRefsFromGroups(pluginCfg: PluginData) {
60-
if (pluginCfg?.groups?.length && pluginCfg?.audits?.length) {
61-
const groups = pluginCfg?.groups || [];
62-
const audits = pluginCfg?.audits || [];
63-
return hasMissingStrings(
64-
groups.flatMap(({ refs: audits }) => audits.map(({ slug: ref }) => ref)),
65-
audits.map(({ slug }) => slug),
66-
);
67-
}
68-
return false;
60+
return hasMissingStrings(
61+
pluginCfg.groups?.flatMap(({ refs: audits }) =>
62+
audits.map(({ slug: ref }) => ref),
63+
) ?? [],
64+
pluginCfg.audits.map(({ slug }) => slug),
65+
);
6966
}
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,109 @@
11
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';
53

64
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();
1029
});
1130

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();
2041
});
2142

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();
3054
});
3155

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",
3874
);
3975
});
4076

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",
5095
);
5196
});
5297

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');
100108
});
101109
});

0 commit comments

Comments
 (0)