Skip to content

Commit 58ba2b1

Browse files
committed
test(models): improve test coverage and quality
1 parent 8d16719 commit 58ba2b1

7 files changed

Lines changed: 408 additions & 86 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { Issue, issueSchema } from './audit-issue';
3+
4+
describe('issueSchema', () => {
5+
it('should accept a valid issue without source file information', () => {
6+
expect(() =>
7+
issueSchema.parse({
8+
message: 'Do not use console.log()',
9+
severity: 'error',
10+
} satisfies Issue),
11+
).not.toThrow();
12+
});
13+
14+
it('should accept a valid issue with source file information', () => {
15+
expect(() =>
16+
issueSchema.parse({
17+
message: 'Use const instead of let.',
18+
severity: 'error',
19+
source: {
20+
file: 'my/code/index.ts',
21+
position: { startLine: 0, startColumn: 4, endLine: 1, endColumn: 10 },
22+
},
23+
} satisfies Issue),
24+
).not.toThrow();
25+
});
26+
27+
it('should throw for a missing message', () => {
28+
expect(() =>
29+
issueSchema.parse({
30+
severity: 'error',
31+
source: { file: 'my/code/index.ts' },
32+
}),
33+
).toThrow('invalid_type');
34+
});
35+
36+
it('should throw for an invalid issue severity', () => {
37+
expect(() =>
38+
issueSchema.parse({
39+
message: 'Use const instead of let.',
40+
severity: 'critical',
41+
}),
42+
).toThrow('Invalid enum value');
43+
});
44+
});
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { describe, expect, it } from 'vitest';
2+
import {
3+
AuditOutput,
4+
AuditOutputs,
5+
auditOutputSchema,
6+
auditOutputsSchema,
7+
} from './audit-output';
8+
9+
describe('auditOutputSchema', () => {
10+
it('should accept a valid audit output without details', () => {
11+
expect(() =>
12+
auditOutputSchema.parse({
13+
slug: 'cypress-e2e-test-results',
14+
score: 0.8,
15+
value: 80,
16+
displayValue: '80 %',
17+
} satisfies AuditOutput),
18+
).not.toThrow();
19+
});
20+
21+
it('should accept a valid audit output with details', () => {
22+
expect(() =>
23+
auditOutputSchema.parse({
24+
slug: 'speed-index',
25+
score: 0.3,
26+
value: 4500,
27+
displayValue: '4.5 s',
28+
details: {
29+
issues: [
30+
{
31+
message: 'The progress chart was blocked for 4 seconds.',
32+
severity: 'info',
33+
},
34+
],
35+
},
36+
} satisfies AuditOutput),
37+
).not.toThrow();
38+
});
39+
40+
it('should throw for a negative value', () => {
41+
expect(() =>
42+
auditOutputSchema.parse({
43+
slug: 'speed-index',
44+
score: 1,
45+
value: -100,
46+
} satisfies AuditOutput),
47+
).toThrow('too_small');
48+
});
49+
50+
it('should throw for a missing score', () => {
51+
expect(() =>
52+
auditOutputSchema.parse({
53+
slug: 'total-blocking-time',
54+
value: 2500,
55+
}),
56+
).toThrow('invalid_type');
57+
});
58+
59+
it('should throw for an invalid slug', () => {
60+
expect(() =>
61+
auditOutputSchema.parse({
62+
slug: 'Lighthouse',
63+
value: 2500,
64+
}),
65+
).toThrow('slug has to follow the pattern');
66+
});
67+
});
68+
69+
describe('auditOutputsSchema', () => {
70+
it('should accept a valid audit output array', () => {
71+
expect(() =>
72+
auditOutputsSchema.parse([
73+
{
74+
slug: 'total-blocking-time',
75+
value: 2500,
76+
score: 0.8,
77+
},
78+
{
79+
slug: 'speed-index',
80+
score: 1,
81+
value: 250,
82+
},
83+
] satisfies AuditOutputs),
84+
).not.toThrow();
85+
});
86+
87+
it('should accept an empty output array', () => {
88+
expect(() =>
89+
auditOutputsSchema.parse([] satisfies AuditOutputs),
90+
).not.toThrow();
91+
});
92+
93+
it('should throw for duplicate outputs', () => {
94+
expect(() =>
95+
auditOutputsSchema.parse([
96+
{
97+
slug: 'total-blocking-time',
98+
value: 2500,
99+
score: 0.8,
100+
},
101+
{
102+
slug: 'speed-index',
103+
score: 1,
104+
value: 250,
105+
},
106+
{
107+
slug: 'total-blocking-time',
108+
value: 4300,
109+
score: 0.75,
110+
},
111+
] satisfies AuditOutputs),
112+
).toThrow('slugs are not unique: total-blocking-time');
113+
});
114+
});
Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,71 @@
11
import { describe, expect, it } from 'vitest';
2-
import { reportMock } from '../../test';
3-
import { auditOutputsSchema } from './audit-output';
4-
import { pluginConfigSchema } from './plugin-config';
2+
import { Audit, auditSchema, pluginAuditsSchema } from './audit';
53

6-
describe('auditOutputsSchema', () => {
7-
it('should pass if output audits are valid', () => {
8-
const auditOutputs = reportMock().plugins[0].audits;
9-
expect(() => auditOutputsSchema.parse(auditOutputs)).not.toThrow();
4+
describe('auditSchema', () => {
5+
it('should accept a valid audit with all information', () => {
6+
expect(() =>
7+
auditSchema.parse({
8+
slug: 'no-conditionals-in-tests',
9+
title: 'No conditional logic is used in tests.',
10+
description: 'Conditional logic does not produce stable results.',
11+
docsUrl:
12+
'https://github.com/jest-community/eslint-plugin-jest/blob/main/docs/rules/no-conditional-in-test.md',
13+
} satisfies Audit),
14+
).not.toThrow();
1015
});
1116

12-
it('should throw if slugs of audits are invalid', () => {
13-
const auditOutputs = reportMock().plugins[0].audits;
14-
auditOutputs[0].slug = '-invalid-audit-slug';
15-
16-
expect(() => auditOutputsSchema.parse(auditOutputs)).toThrow(
17-
'slug has to follow the pattern',
18-
);
17+
it('should accept a valid audit with minimum information', () => {
18+
expect(() =>
19+
auditSchema.parse({
20+
slug: 'jest-unit-test-results',
21+
title: 'Jest unit tests results.',
22+
} satisfies Audit),
23+
).not.toThrow();
1924
});
2025

21-
it('should throw if slugs of audits are duplicated', () => {
22-
const audits = reportMock().plugins[0].audits;
23-
const auditOutputs = [...audits, audits[0]];
24-
expect(() => auditOutputsSchema.parse(auditOutputs)).toThrow(
25-
'In plugin audits the slugs are not unique',
26-
);
26+
it('should throw for an invalid URL', () => {
27+
expect(() =>
28+
auditSchema.parse({
29+
slug: 'consistent-test-it',
30+
title: 'Use a consistent test function.',
31+
docsUrl: 'invalid-url',
32+
} satisfies Audit),
33+
).toThrow('Invalid url');
2734
});
35+
});
2836

29-
it('should throw if plugin groups refs contain invalid slugs', () => {
30-
const invalidAuditRef = '-invalid-audit-ref';
31-
const pluginConfig = reportMock().plugins[1];
32-
const groups = pluginConfig.groups;
33-
groups[0].refs[0].slug = invalidAuditRef;
34-
pluginConfig.groups = groups;
37+
describe('pluginAuditsSchema', () => {
38+
it('should parse a valid audit array', () => {
39+
expect(() =>
40+
pluginAuditsSchema.parse([
41+
{
42+
slug: 'consistent-test-it',
43+
title: 'Use a consistent test function.',
44+
},
45+
{
46+
slug: 'jest-unit-test-results',
47+
title: 'Jest unit tests results.',
48+
},
49+
] satisfies Audit[]),
50+
).not.toThrow();
51+
});
3552

36-
expect(() => pluginConfigSchema.parse(pluginConfig)).toThrow(
37-
`slug has to follow the patter`,
38-
);
53+
it('should throw for duplicate audits', () => {
54+
expect(() =>
55+
pluginAuditsSchema.parse([
56+
{
57+
slug: 'consistent-test-it',
58+
title: 'Use a consistent test function.',
59+
},
60+
{
61+
slug: 'jest-unit-test-results',
62+
title: 'Jest unit tests results.',
63+
},
64+
{
65+
slug: 'jest-unit-test-results',
66+
title: 'Jest unit tests results.',
67+
},
68+
] satisfies Audit[]),
69+
).toThrow('slugs are not unique: jest-unit-test-results');
3970
});
4071
});

0 commit comments

Comments
 (0)