Skip to content

Commit ce21d87

Browse files
authored
refactor(chore): improvements testing (#46)
This PR includes: - a refactoring for the test helper to mock schemas - updated imports - adoptions to the schemas (removes `label`)
1 parent ba048be commit ce21d87

File tree

17 files changed

+3116
-1712
lines changed

17 files changed

+3116
-1712
lines changed

package-lock.json

Lines changed: 3043 additions & 1525 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/src/lib/collect/command-object.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ function mockPlugin(): PluginConfig {
6161
slug: 'command-object-audit-slug',
6262
title: 'audit title',
6363
description: 'audit description',
64-
label: 'mock audit label',
6564
docsUrl: 'http://www.my-docs.dev',
6665
},
6766
],

packages/models/src/lib/category-config.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest';
22
import { categoryConfigSchema } from './category-config';
3-
import { mockCategory } from './implementation/helpers.mock';
3+
import { mockCategory } from '../../test';
44

55
describe('categoryConfigSchema', () => {
66
it('should parse if configuration with audit refs is valid', () => {

packages/models/src/lib/core-config.spec.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { describe, expect, it } from 'vitest';
22
import { coreConfigSchema } from './core-config';
3-
import {
4-
mockCategory,
5-
mockConfig,
6-
mockPluginConfig,
7-
} from './implementation/helpers.mock';
3+
import { mockCategory, mockConfig, mockPluginConfig } from '../../test';
84

95
/*
106
- plugin slug: es-lint

packages/models/src/lib/implementation/schemas.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export function weightSchema(
8181
export function positiveIntSchema(description: string) {
8282
return z.number({ description }).int().nonnegative();
8383
}
84+
8485
/**
8586
* Schema for a unixFilePath
8687
* @param description
@@ -89,6 +90,24 @@ export function unixFilePathSchema(description: string) {
8990
return z.string({ description }).regex(unixFilePathRegex);
9091
}
9192

93+
export function packageVersionSchema(options?: {
94+
versionDescription?: string;
95+
optional?: boolean;
96+
}) {
97+
let { versionDescription, optional } = options || {};
98+
versionDescription = versionDescription || 'NPM version of the package';
99+
optional = !!optional;
100+
const packageSchema = z.string({ description: 'NPM package name' });
101+
const versionSchema = z.string({ description: versionDescription });
102+
return z.object(
103+
{
104+
package: optional ? packageSchema.optional() : packageSchema,
105+
version: optional ? versionSchema.optional() : versionSchema,
106+
},
107+
{ description: 'NPM package name and version of a published package' },
108+
);
109+
}
110+
92111
export function weightedRefSchema(
93112
description: string,
94113
slugDescription: string,

packages/models/src/lib/persist-config.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest';
2-
import { mockPersistConfig } from './implementation/helpers.mock';
2+
import { mockPersistConfig } from '../../test';
33
import { persistConfigSchema } from './persist-config';
44

55
describe('persistConfigSchema', () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
mockGroupConfig,
44
mockPluginConfig,
55
mockRunnerOutput,
6-
} from './implementation/helpers.mock';
6+
} from '../../test';
77
import {
88
auditGroupSchema,
99
pluginConfigSchema,

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

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
descriptionSchema,
44
docsUrlSchema,
55
generalFilePathSchema,
6+
packageVersionSchema,
67
positiveIntSchema,
78
scorableSchema,
89
slugSchema,
@@ -18,22 +19,26 @@ import {
1819
} from './implementation/utils';
1920

2021
// Define Zod schema for the PluginMetadata type
21-
export const pluginMetadataSchema = z.object(
22-
{
23-
slug: slugSchema(),
24-
name: z
25-
.string({
26-
description: 'Display name',
27-
})
28-
.max(128),
29-
icon: z.union([z.unknown(), z.string()], {
30-
description: 'Icon from VSCode Material Icons extension',
31-
}),
32-
docsUrl: docsUrlSchema('Plugin documentation site'),
33-
},
34-
{
35-
description: 'Plugin metadata',
36-
},
22+
export const pluginMetadataSchema = packageVersionSchema({
23+
optional: true,
24+
}).merge(
25+
z.object(
26+
{
27+
slug: slugSchema(),
28+
name: z
29+
.string({
30+
description: 'Display name',
31+
})
32+
.max(128),
33+
icon: z.union([z.unknown(), z.string()], {
34+
description: 'Icon from VSCode Material Icons extension',
35+
}),
36+
docsUrl: docsUrlSchema('Plugin documentation site'),
37+
},
38+
{
39+
description: 'Plugin metadata',
40+
},
41+
),
3742
);
3843

3944
// Define Zod schema for the RunnerConfig type
@@ -54,11 +59,6 @@ const runnerConfigSchema = z.object(
5459
export const auditMetadataSchema = z.object(
5560
{
5661
slug: slugSchema('ID (unique within plugin)'),
57-
label: z
58-
.string({
59-
description: 'Abbreviated name',
60-
})
61-
.max(128),
6262
title: titleSchema('Descriptive name'),
6363
description: descriptionSchema('Description (Markdown)'),
6464
docsUrl: docsUrlSchema('Link to documentation (rationale)'),
@@ -138,27 +138,6 @@ export const pluginConfigSchema = z
138138

139139
export type PluginConfig = z.infer<typeof pluginConfigSchema>;
140140

141-
/**
142-
* Define Zod schema for the SourceFileLocation type.
143-
*
144-
* @example
145-
*
146-
* // Example data for the RunnerOutput type
147-
* const runnerOutputData = {
148-
* audits: [
149-
* // ... populate with example audit data ...
150-
* ],
151-
* };
152-
*
153-
* // Validate the data against the schema
154-
* const validationResult = runnerOutputSchema.safeParse(runnerOutputData);
155-
*
156-
* if (validationResult.success) {
157-
* console.log('Valid runner output:', validationResult.data);
158-
* } else {
159-
* console.error('Invalid runner output:', validationResult.error);
160-
* }
161-
*/
162141
const sourceFileLocationSchema = z.object(
163142
{
164143
file: unixFilePathSchema('Relative path to source file in Git repo'),

packages/models/src/lib/report.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { describe, expect, it } from 'vitest';
2-
import {
3-
mockPluginConfig,
4-
mockRunnerOutput,
5-
} from './implementation/helpers.mock';
2+
import { mockPluginConfig, mockRunnerOutput } from '../../test';
63
import { runnerOutputAuditRefsPresentInPluginConfigs } from './report';
74

85
describe('RunnerOutput', () => {

packages/models/src/lib/report.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
auditResultSchema,
88
pluginMetadataSchema,
99
} from './plugin-config';
10+
import { packageVersionSchema } from './implementation/schemas';
1011

1112
export type PluginOutput = RunnerOutput & {
1213
slug: string;
@@ -25,15 +26,17 @@ export const pluginReportSchema = z.object({
2526
});
2627
export type PluginReport = z.infer<typeof pluginReportSchema>;
2728

28-
export const reportSchema = z.object(
29-
{
30-
package: z.string({ description: 'NPM package name' }),
31-
version: z.string({ description: 'NPM version of the CLI' }),
32-
date: z.string({ description: 'Start date and time of the collect run' }),
33-
duration: z.number({ description: 'Duration of the collect run in ms' }),
34-
plugins: z.array(pluginReportSchema),
35-
},
36-
{ description: 'Collect output data.' },
29+
export const reportSchema = packageVersionSchema({
30+
versionDescription: 'NPM version of the CLI',
31+
}).merge(
32+
z.object(
33+
{
34+
date: z.string({ description: 'Start date and time of the collect run' }),
35+
duration: z.number({ description: 'Duration of the collect run in ms' }),
36+
plugins: z.array(pluginReportSchema),
37+
},
38+
{ description: 'Collect output data.' },
39+
),
3740
);
3841

3942
export type Report = z.infer<typeof reportSchema>;

0 commit comments

Comments
 (0)