Skip to content

Commit

Permalink
test: add type notation in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlacenka committed Mar 7, 2024
1 parent dab5822 commit ee57ed9
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
PERSIST_FILENAME,
PERSIST_FORMAT,
PERSIST_OUTPUT_DIR,
PersistConfig,
UploadConfig,
} from '@code-pushup/models';
import { CORE_CONFIG_MOCK, MINIMAL_CONFIG_MOCK } from '@code-pushup/test-utils';
import { yargsCli } from '../yargs-cli';
Expand Down Expand Up @@ -58,7 +60,7 @@ describe('parsing values from CLI and middleware', () => {
},
).parseAsync();

expect(persist).toEqual({
expect(persist).toEqual<PersistConfig>({
filename: PERSIST_FILENAME,
format: PERSIST_FORMAT,
outputDir: PERSIST_OUTPUT_DIR,
Expand All @@ -79,7 +81,7 @@ describe('parsing values from CLI and middleware', () => {
},
).parseAsync();

expect(persist).toEqual({
expect(persist).toEqual<PersistConfig>({
filename: 'cli-filename',
format: ['md'],
outputDir: 'cli-outputDir',
Expand All @@ -95,7 +97,7 @@ describe('parsing values from CLI and middleware', () => {
},
).parseAsync();

expect(persist).toEqual({
expect(persist).toEqual<PersistConfig>({
filename: 'rc-filename',
format: ['json', 'md'],
outputDir: 'rc-outputDir',
Expand All @@ -116,7 +118,7 @@ describe('parsing values from CLI and middleware', () => {
},
).parseAsync();

expect(persist).toEqual({
expect(persist).toEqual<PersistConfig>({
filename: 'cli-filename',
format: ['md'],
outputDir: 'cli-outputDir',
Expand All @@ -135,7 +137,7 @@ describe('parsing values from CLI and middleware', () => {
},
).parseAsync();

expect(persist).toEqual({
expect(persist).toEqual<PersistConfig>({
filename: 'rc-filename',
format: PERSIST_FORMAT,
outputDir: 'cli-outputdir',
Expand Down Expand Up @@ -163,7 +165,7 @@ describe('parsing values from CLI and middleware', () => {
},
).parseAsync();

expect(upload).toStrictEqual({
expect(upload).toStrictEqual<UploadConfig>({
organization: 'code-pushup',
project: 'portal',
apiKey: 'dummy-api-key',
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/lib/yargs-cli.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { CoreConfig } from '@code-pushup/models';
import { CoreConfig, Format } from '@code-pushup/models';
import {
PersistConfigCliOptions,
UploadConfigCliOptions,
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('yargsCli', () => {
['--persist.format=md', '--persist.format=json'],
{ options },
).parseAsync();
expect(parsedArgv.persist?.format).toEqual(['md', 'json']);
expect(parsedArgv.persist?.format).toEqual<Format[]>(['md', 'json']);
});

it('should throw for an invalid persist format', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-coverage/src/lib/config.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ describe('coveragePluginConfigSchema', () => {
expect(() => coveragePluginConfigSchema.parse(config)).not.toThrow();

const { coverageTypes } = coveragePluginConfigSchema.parse(config);
expect(coverageTypes).toEqual([
expect(coverageTypes).toEqual<CoverageType[]>([
'function',
'branch',
'line',
] satisfies CoverageType[]);
]);
});

it('throws for empty coverage type array', () => {
Expand Down
46 changes: 33 additions & 13 deletions packages/plugin-coverage/src/lib/runner/lcov/transform.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LCOVRecord } from 'parse-lcov';
import { describe, it } from 'vitest';
import { AuditOutput, Issue } from '@code-pushup/models';
import type { AuditOutput, Issue } from '@code-pushup/models';
import {
lcovCoverageToAuditOutput,
lcovReportToBranchStat,
Expand Down Expand Up @@ -28,7 +28,7 @@ describe('lcovReportToFunctionStat', () => {
details: [{ line: 12, name: 'yargsCli', hit: 6 }],
},
}),
).toEqual({ totalHit: 1, totalFound: 1, issues: [] } satisfies LCOVStat);
).toEqual<LCOVStat>({ totalHit: 1, totalFound: 1, issues: [] });
});

it('should transform an empty LCOV function report to LCOV stat', () => {
Expand All @@ -37,7 +37,11 @@ describe('lcovReportToFunctionStat', () => {
...lcovRecordMock,
functions: { hit: 0, found: 0, details: [] },
}),
).toEqual({ totalHit: 0, totalFound: 0, issues: [] } satisfies LCOVStat);
).toEqual<LCOVStat>({
totalHit: 0,
totalFound: 0,
issues: [],
});
});

it('should transform details from function report to issues', () => {
Expand Down Expand Up @@ -101,7 +105,11 @@ describe('lcovReportToLineStat', () => {
details: [{ line: 1, hit: 6 }],
},
}),
).toEqual({ totalHit: 1, totalFound: 1, issues: [] } satisfies LCOVStat);
).toEqual<LCOVStat>({
totalHit: 1,
totalFound: 1,
issues: [],
});
});

it('should transform an empty LCOV line report to LCOV stat', () => {
Expand All @@ -110,7 +118,11 @@ describe('lcovReportToLineStat', () => {
...lcovRecordMock,
lines: { hit: 0, found: 0, details: [] },
}),
).toEqual({ totalHit: 0, totalFound: 0, issues: [] } satisfies LCOVStat);
).toEqual<LCOVStat>({
totalHit: 0,
totalFound: 0,
issues: [],
});
});

it('should transform details from line report to issues', () => {
Expand Down Expand Up @@ -210,7 +222,11 @@ describe('lcovReportToBranchStat', () => {
details: [{ line: 12, taken: 6, branch: 0, block: 0 }],
},
}),
).toEqual({ totalHit: 1, totalFound: 1, issues: [] } satisfies LCOVStat);
).toEqual<LCOVStat>({
totalHit: 1,
totalFound: 1,
issues: [],
});
});

it('should transform an empty LCOV branch report to LCOV stat', () => {
Expand All @@ -219,7 +235,11 @@ describe('lcovReportToBranchStat', () => {
...lcovRecordMock,
branches: { hit: 0, found: 0, details: [] },
}),
).toEqual({ totalHit: 0, totalFound: 0, issues: [] } satisfies LCOVStat);
).toEqual<LCOVStat>({
totalHit: 0,
totalFound: 0,
issues: [],
});
});

it('should transform details from branch report to issues', () => {
Expand Down Expand Up @@ -279,12 +299,12 @@ describe('lcovCoverageToAudit', () => {
{ totalHit: 56, totalFound: 56, issues: [] },
'branch',
),
).toEqual({
).toEqual<AuditOutput>({
slug: 'branch-coverage',
score: 1,
value: 100,
displayValue: '100 %',
} satisfies AuditOutput);
});
});

it('should transform an empty function coverage to audit output', () => {
Expand All @@ -293,12 +313,12 @@ describe('lcovCoverageToAudit', () => {
{ totalHit: 0, totalFound: 0, issues: [] },
'function',
),
).toEqual({
).toEqual<AuditOutput>({
slug: 'function-coverage',
score: 1,
value: 100,
displayValue: '100 %',
} satisfies AuditOutput);
});
});

it('should transform a partial line coverage to audit output', () => {
Expand All @@ -317,7 +337,7 @@ describe('lcovCoverageToAudit', () => {
},
'line',
),
).toEqual({
).toEqual<AuditOutput>({
slug: 'line-coverage',
score: 0.9,
value: 90,
Expand All @@ -331,6 +351,6 @@ describe('lcovCoverageToAudit', () => {
},
],
},
} satisfies AuditOutput);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ describe('createRunnerConfig', () => {
coverageTypes: ['branch'],
perfectScoreThreshold: 85,
});
expect(runnerConfig).toStrictEqual({
expect(runnerConfig).toStrictEqual<RunnerConfig>({
command: 'node',
args: ['executeRunner.ts'],
outputTransform: expect.any(Function),
outputFile: expect.stringContaining('runner-output.json'),
} satisfies RunnerConfig);
});
});

it('should provide plugin config to runner in JSON file', async () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/plugin-coverage/src/lib/utils.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ describe('applyMaxScoreAboveThreshold', () => {
slug: 'branch-coverage',
value: 75,
score: 0.75,
} satisfies AuditOutput,
},
],
0.7,
),
).toEqual([
).toEqual<AuditOutput[]>([
{
slug: 'branch-coverage',
value: 75,
score: 1,
} satisfies AuditOutput,
},
]);
});

Expand All @@ -32,16 +32,16 @@ describe('applyMaxScoreAboveThreshold', () => {
slug: 'line-coverage',
value: 60,
score: 0.6,
} satisfies AuditOutput,
},
],
0.7,
),
).toEqual([
).toEqual<AuditOutput[]>([
{
slug: 'line-coverage',
value: 60,
score: 0.6,
} satisfies AuditOutput,
},
]);
});
});
6 changes: 3 additions & 3 deletions packages/plugin-eslint/src/lib/runner/transform.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ESLint } from 'eslint';
import type { AuditOutput } from '@code-pushup/models';
import { AuditOutput } from '@code-pushup/models';
import { lintResultsToAudits } from './transform';

describe('lintResultsToAudits', () => {
Expand Down Expand Up @@ -109,7 +109,7 @@ describe('lintResultsToAudits', () => {
},
},
}),
).toEqual([
).toEqual<AuditOutput[]>([
{
slug: expect.stringContaining('max-lines'),
score: 0,
Expand Down Expand Up @@ -221,6 +221,6 @@ describe('lintResultsToAudits', () => {
],
},
},
] satisfies AuditOutput[]);
]);
});
});
19 changes: 10 additions & 9 deletions packages/plugin-lighthouse/src/lib/utils.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Details from 'lighthouse/types/lhr/audit-details';
import { describe, expect, it } from 'vitest';
import {
Audit,
AuditOutput,
Group,
PluginConfig,
pluginConfigSchema,
Expand Down Expand Up @@ -152,10 +153,10 @@ describe('filterAuditsAndGroupsByOnlyOptions to be used in plugin config', () =>
{ onlyAudits: ['speed-index'] },
);

expect(filteredAudits).toStrictEqual([
expect(filteredAudits).toStrictEqual<Audit[]>([
{ slug: 'speed-index', title: 'Speed Index' },
]);
expect(filteredGroups).toStrictEqual([
expect(filteredGroups).toStrictEqual<Group[]>([
{
slug: 'performance',
title: 'Performance',
Expand Down Expand Up @@ -195,10 +196,10 @@ describe('filterAuditsAndGroupsByOnlyOptions to be used in plugin config', () =>
],
{ onlyAudits: ['speed-index'] },
);
expect(filteredAudits).toStrictEqual([
expect(filteredAudits).toStrictEqual<Audit[]>([
{ slug: 'speed-index', title: 'Speed Index' },
]);
expect(filteredGroups).toStrictEqual([
expect(filteredGroups).toStrictEqual<Group[]>([
{
slug: 'performance',
title: 'Performance',
Expand Down Expand Up @@ -245,10 +246,10 @@ describe('filterAuditsAndGroupsByOnlyOptions to be used in plugin config', () =>
{ onlyCategories: ['coverage'] },
);

expect(filteredAudits).toStrictEqual([
expect(filteredAudits).toStrictEqual<Audit[]>([
{ slug: 'function-coverage', title: 'Function Coverage' },
]);
expect(filteredGroups).toStrictEqual([
expect(filteredGroups).toStrictEqual<Group[]>([
{
slug: 'coverage',
title: 'Code coverage',
Expand Down Expand Up @@ -298,10 +299,10 @@ describe('filterAuditsAndGroupsByOnlyOptions to be used in plugin config', () =>
},
);

expect(filteredAudits).toStrictEqual([
expect(filteredAudits).toStrictEqual<Audit[]>([
{ slug: 'function-coverage', title: 'Function Coverage' },
]);
expect(filteredGroups).toStrictEqual([
expect(filteredGroups).toStrictEqual<Group[]>([
{
slug: 'coverage',
title: 'Code coverage',
Expand Down Expand Up @@ -378,7 +379,7 @@ describe('toAuditOutputs', () => {
displayValue: '2.8 s',
},
]),
).toStrictEqual([
).toStrictEqual<AuditOutput[]>([
{
displayValue: '2.8 s',
score: 0.55,
Expand Down
Loading

0 comments on commit ee57ed9

Please sign in to comment.