Skip to content

Commit

Permalink
test(models): improve test coverage and quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlacenka committed Jan 8, 2024
1 parent 8d16719 commit 58ba2b1
Show file tree
Hide file tree
Showing 7 changed files with 408 additions and 86 deletions.
44 changes: 44 additions & 0 deletions packages/models/src/lib/audit-issue.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, it } from 'vitest';
import { Issue, issueSchema } from './audit-issue';

describe('issueSchema', () => {
it('should accept a valid issue without source file information', () => {
expect(() =>
issueSchema.parse({
message: 'Do not use console.log()',
severity: 'error',
} satisfies Issue),
).not.toThrow();
});

it('should accept a valid issue with source file information', () => {
expect(() =>
issueSchema.parse({
message: 'Use const instead of let.',
severity: 'error',
source: {
file: 'my/code/index.ts',
position: { startLine: 0, startColumn: 4, endLine: 1, endColumn: 10 },
},
} satisfies Issue),
).not.toThrow();
});

it('should throw for a missing message', () => {
expect(() =>
issueSchema.parse({
severity: 'error',
source: { file: 'my/code/index.ts' },
}),
).toThrow('invalid_type');
});

it('should throw for an invalid issue severity', () => {
expect(() =>
issueSchema.parse({
message: 'Use const instead of let.',
severity: 'critical',
}),
).toThrow('Invalid enum value');
});
});
114 changes: 114 additions & 0 deletions packages/models/src/lib/audit-output.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { describe, expect, it } from 'vitest';
import {
AuditOutput,
AuditOutputs,
auditOutputSchema,
auditOutputsSchema,
} from './audit-output';

describe('auditOutputSchema', () => {
it('should accept a valid audit output without details', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'cypress-e2e-test-results',
score: 0.8,
value: 80,
displayValue: '80 %',
} satisfies AuditOutput),
).not.toThrow();
});

it('should accept a valid audit output with details', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'speed-index',
score: 0.3,
value: 4500,
displayValue: '4.5 s',
details: {
issues: [
{
message: 'The progress chart was blocked for 4 seconds.',
severity: 'info',
},
],
},
} satisfies AuditOutput),
).not.toThrow();
});

it('should throw for a negative value', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'speed-index',
score: 1,
value: -100,
} satisfies AuditOutput),
).toThrow('too_small');
});

it('should throw for a missing score', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'total-blocking-time',
value: 2500,
}),
).toThrow('invalid_type');
});

it('should throw for an invalid slug', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'Lighthouse',
value: 2500,
}),
).toThrow('slug has to follow the pattern');
});
});

describe('auditOutputsSchema', () => {
it('should accept a valid audit output array', () => {
expect(() =>
auditOutputsSchema.parse([
{
slug: 'total-blocking-time',
value: 2500,
score: 0.8,
},
{
slug: 'speed-index',
score: 1,
value: 250,
},
] satisfies AuditOutputs),
).not.toThrow();
});

it('should accept an empty output array', () => {
expect(() =>
auditOutputsSchema.parse([] satisfies AuditOutputs),
).not.toThrow();
});

it('should throw for duplicate outputs', () => {
expect(() =>
auditOutputsSchema.parse([
{
slug: 'total-blocking-time',
value: 2500,
score: 0.8,
},
{
slug: 'speed-index',
score: 1,
value: 250,
},
{
slug: 'total-blocking-time',
value: 4300,
score: 0.75,
},
] satisfies AuditOutputs),
).toThrow('slugs are not unique: total-blocking-time');
});
});
89 changes: 60 additions & 29 deletions packages/models/src/lib/audit.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,71 @@
import { describe, expect, it } from 'vitest';
import { reportMock } from '../../test';
import { auditOutputsSchema } from './audit-output';
import { pluginConfigSchema } from './plugin-config';
import { Audit, auditSchema, pluginAuditsSchema } from './audit';

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

it('should throw if slugs of audits are invalid', () => {
const auditOutputs = reportMock().plugins[0].audits;
auditOutputs[0].slug = '-invalid-audit-slug';

expect(() => auditOutputsSchema.parse(auditOutputs)).toThrow(
'slug has to follow the pattern',
);
it('should accept a valid audit with minimum information', () => {
expect(() =>
auditSchema.parse({
slug: 'jest-unit-test-results',
title: 'Jest unit tests results.',
} satisfies Audit),
).not.toThrow();
});

it('should throw if slugs of audits are duplicated', () => {
const audits = reportMock().plugins[0].audits;
const auditOutputs = [...audits, audits[0]];
expect(() => auditOutputsSchema.parse(auditOutputs)).toThrow(
'In plugin audits the slugs are not unique',
);
it('should throw for an invalid URL', () => {
expect(() =>
auditSchema.parse({
slug: 'consistent-test-it',
title: 'Use a consistent test function.',
docsUrl: 'invalid-url',
} satisfies Audit),
).toThrow('Invalid url');
});
});

it('should throw if plugin groups refs contain invalid slugs', () => {
const invalidAuditRef = '-invalid-audit-ref';
const pluginConfig = reportMock().plugins[1];
const groups = pluginConfig.groups;
groups[0].refs[0].slug = invalidAuditRef;
pluginConfig.groups = groups;
describe('pluginAuditsSchema', () => {
it('should parse a valid audit array', () => {
expect(() =>
pluginAuditsSchema.parse([
{
slug: 'consistent-test-it',
title: 'Use a consistent test function.',
},
{
slug: 'jest-unit-test-results',
title: 'Jest unit tests results.',
},
] satisfies Audit[]),
).not.toThrow();
});

expect(() => pluginConfigSchema.parse(pluginConfig)).toThrow(
`slug has to follow the patter`,
);
it('should throw for duplicate audits', () => {
expect(() =>
pluginAuditsSchema.parse([
{
slug: 'consistent-test-it',
title: 'Use a consistent test function.',
},
{
slug: 'jest-unit-test-results',
title: 'Jest unit tests results.',
},
{
slug: 'jest-unit-test-results',
title: 'Jest unit tests results.',
},
] satisfies Audit[]),
).toThrow('slugs are not unique: jest-unit-test-results');
});
});
Loading

0 comments on commit 58ba2b1

Please sign in to comment.