Skip to content

Commit

Permalink
test: Adds tests for FileCoverageHtml, creates mockFactories and refa…
Browse files Browse the repository at this point in the history
…ctors tests for SummaryTable (#15)

* test: Adds tests for FileCoverageHtml, creates mockFactories and refactors tests for SummaryTable
* chore: Removes non-required export
  • Loading branch information
davelosert authored Jan 3, 2023
1 parent 12c6cfe commit 4f2bca0
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 87 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,4 @@ typings/
.tern-port

!test/mockReports/coverage
tmp
56 changes: 56 additions & 0 deletions src/generateFileCoverageHtml.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { createJsonFinalEntry } from './types/JsonFinalMockFactory';
import { generateFileCoverageHtml } from './generateFileCoverageHtml';
import { getTableLine } from '../test/queryHelper';
import { JsonFinal } from './types/JsonFinal';
import { JsonSummary } from './types/JsonSummary';
import { createMockCoverageReport, createMockJsonSummary, createMockReportNumbers } from './types/JsonSummaryMockFactory';
import { describe, it, expect } from 'vitest';

describe('generateFileCoverageHtml()', () => {
it('renders the statements, branches, functions and line coverage-percentage of a file.', () => {
const jsonSummary: JsonSummary = createMockJsonSummary({
'src/generateFileCoverageHtml.ts': {
statements: createMockReportNumbers({ pct: 70, }),
branches: createMockReportNumbers({ pct: 80, }),
functions: createMockReportNumbers({ pct: 90, }),
lines: createMockReportNumbers({ pct: 100, }),
},
});

const html = generateFileCoverageHtml({
jsonSummary,
jsonFinal: {}
});

const firstTableLine = getTableLine(1, html);

expect(firstTableLine).toContain('70%');
expect(firstTableLine).toContain('80%');
expect(firstTableLine).toContain('90%');
expect(firstTableLine).toContain('100%');
});

it('renders the line-coverage in the same row as the coverage.', async (): Promise<void> => {
const jsonSummary: JsonSummary = createMockJsonSummary({
'src/exampleFile.ts': createMockCoverageReport({
statements: createMockReportNumbers({ pct: 70, }),
}),
});
const jsonFinal: JsonFinal = {
...createJsonFinalEntry('src/exampleFile.ts', [
{ line: 1, covered: false },
{ line: 2, covered: false }
]),
};

const html = generateFileCoverageHtml({
jsonSummary,
jsonFinal
});

const firstTableLine = getTableLine(1, html);

expect(firstTableLine).toContain('70%');
expect(firstTableLine).toContain('1-2');
});
});
129 changes: 69 additions & 60 deletions src/generateSummaryTableHtml.test.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,98 @@
import { describe, it, expect } from 'vitest';
import * as core from '@actions/core';
import { generateSummaryTableData, TableData } from './generateSummaryTableHtml';
import { CoverageReport } from './types/JsonSummary';
import { generateSummaryTableHtml } from './generateSummaryTableHtml';
import { getTableLine } from '../test/queryHelper';
import { icons } from './icons';
import { Thresholds } from './types/Threshold';
import { createMockCoverageReport, createMockReportNumbers } from './types/JsonSummaryMockFactory';
import { describe, it, expect } from 'vitest';

describe('generateSummaryTabelHtml', () => {
const mockReport: CoverageReport = {
branches: { covered: 10, skipped: 8, pct: 100, total: 18 },
functions: { covered: 0, skipped: 0, pct: 100, total: 0 },
lines: { covered: 8, skipped: 0, pct: 80, total: 10 },
statements: { covered: 0, skipped: 0, pct: 100, total: 0 }
}

const stringifyTableData = (tableData: TableData) => {
return tableData;
}

describe('generateSummaryTabelHtml()', () => {
it('generates the headline', () => {
const data = generateSummaryTableData(mockReport);
const result = stringifyTableData(data);
const mockReport = createMockCoverageReport();
const summaryHtml = generateSummaryTableHtml(mockReport);
const headline = getTableLine(0, summaryHtml);

expect(result).toContain('Status');
expect(result).toContain('Category');
expect(result).toContain('Percentage');
expect(result).toContain('Covered / Total');
expect(headline).toContain('Status');
expect(headline).toContain('Category');
expect(headline).toContain('Percentage');
expect(headline).toContain('Covered / Total');
});

it('generates all categories as rows', async (): Promise<void> => {
const mockReport = createMockCoverageReport();
const summaryHtml = generateSummaryTableHtml(mockReport);

expect(getTableLine(1, summaryHtml)).toContain('Lines');
expect(getTableLine(2, summaryHtml)).toContain('Statements');
expect(getTableLine(3, summaryHtml)).toContain('Functions');
expect(getTableLine(4, summaryHtml)).toContain('Branches');
});


it('adds status blue-circle if no threshold provided.', async (): Promise<void> => {
const tableData = generateSummaryTableData(mockReport);
const result = stringifyTableData(tableData);
const mockReport = createMockCoverageReport();
const summaryHtml = generateSummaryTableHtml(mockReport);

expect(result).toContain(icons.blue);
expect(summaryHtml).toContain(icons.blue);
});

it('adds green-circle if percentage is below threshold.', async (): Promise<void> => {
const data = generateSummaryTableData(mockReport, {
lines: 80

it('adds the percentage with a %-sign.', async (): Promise<void> => {
const mockReport = createMockCoverageReport({
lines: createMockReportNumbers({ pct: 80 })
});

const result = stringifyTableData(data);
const summaryHtml = generateSummaryTableHtml(mockReport);

expect(result).toContain(icons.green);
expect(getTableLine(1, summaryHtml)).toContain('80%');
});

it('adds red-circle if percentage is below threshold.', async (): Promise<void> => {
const data = generateSummaryTableData(mockReport, {
lines: 100
it('shows the covered / total numbers.', async (): Promise<void> => {
const mockReport = createMockCoverageReport({
lines: createMockReportNumbers({
covered: 8,
total: 10,
})
});

const result = stringifyTableData(data);

expect(result).toContain(icons.red);
});

it('shows all categories', async (): Promise<void> => {
const data = generateSummaryTableData(mockReport);
const result = stringifyTableData(data);

expect(result).toContain('Lines');
expect(result).toContain('Statements');
expect(result).toContain('Functions');
expect(result).toContain('Branches');
const summaryHtml = generateSummaryTableHtml(mockReport);

expect(getTableLine(1, summaryHtml)).toContain('8 / 10');
});

it('adds the percentage with a %-sign.', async (): Promise<void> => {
const data = generateSummaryTableData(mockReport);
const result = stringifyTableData(data);
it('adds green-circle if percentage is above threshold.', async (): Promise<void> => {
const thresholds: Thresholds = { lines: 80 };
const mockReport = createMockCoverageReport({
lines: createMockReportNumbers({
pct: 81,
})
});
const summaryHtml = generateSummaryTableHtml(mockReport, thresholds);

expect(result).toContain('80%');
expect(getTableLine(1, summaryHtml)).toContain(icons.green);
});

it('shows the covered / total numbers.', async (): Promise<void> => {
const data = generateSummaryTableData(mockReport);
const result = stringifyTableData(data);
it('adds red-circle if percentage is below threshold.', async (): Promise<void> => {
const thresholds: Thresholds = { lines: 100 };
const mockReport = createMockCoverageReport({
lines: createMockReportNumbers({
pct: 81,
})
});
const summaryHtml = generateSummaryTableHtml(mockReport, thresholds);

expect(result).toContain('8 / 10');
expect(getTableLine(1, summaryHtml)).toContain(icons.red);
});

it('if threshold is given, provides the threshold in the percentage column.', async (): Promise<void> => {
const data = generateSummaryTableData(mockReport, {
lines: 100
const thresholds: Thresholds = { lines: 100 };
const mockReport = createMockCoverageReport({
lines: createMockReportNumbers({
pct: 80,
})
});

const summaryHtml = generateSummaryTableHtml(mockReport, thresholds);

const result = stringifyTableData(data);

expect(result).toContain('80% / 100%');
expect(getTableLine(1, summaryHtml)).toContain('80% / 100%');
});
});
20 changes: 5 additions & 15 deletions src/generateSummaryTableHtml.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { CoverageReport, JsonSummary, ReportNumbers } from './types/JsonSummary';
import { Thresholds } from './types/Threshold';
import core from '@actions/core';
import { oneLine } from 'common-tags';
import { icons } from './icons';

type TableData = string;

import { oneLine } from 'common-tags';
import { Thresholds } from './types/Threshold';
import { CoverageReport, ReportNumbers } from './types/JsonSummary';

const generateTableRow = ({ reportNumbers, category, threshold }: { reportNumbers: ReportNumbers; category: string; threshold?: number; }): string => {

Expand All @@ -26,7 +22,7 @@ const generateTableRow = ({ reportNumbers, category, threshold }: { reportNumber
}


const generateSummaryTableData = (jsonReport: CoverageReport, thresholds: Thresholds = {}): TableData => {
const generateSummaryTableHtml = (jsonReport: CoverageReport, thresholds: Thresholds = {}): string => {
return oneLine`
<table>
<thead>
Expand All @@ -53,14 +49,8 @@ const generateSummaryTableData = (jsonReport: CoverageReport, thresholds: Thresh
</tbody>
</table>
`

}

export {
generateSummaryTableData,
generateTableRow
};

export type {
TableData
generateSummaryTableHtml
};
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateSummaryTableData } from './generateSummaryTableHtml.js';
import { generateSummaryTableHtml } from './generateSummaryTableHtml.js';
import path from 'node:path';
import { parseVitestJsonFinal, parseVitestJsonSummary } from './parseJsonReports.js';
import { writeSummaryToPR } from './writeSummaryToPR.js';
Expand All @@ -18,7 +18,7 @@ const run = async () => {
const jsonFinal = await parseVitestJsonFinal(jsonFinalPath);
const thresholds = await parseCoverageThresholds(viteConfigPath);

const tableData = generateSummaryTableData(jsonSummary.total, thresholds);
const tableData = generateSummaryTableHtml(jsonSummary.total, thresholds);
const fileTable = generateFileCoverageHtml({
jsonSummary, jsonFinal
});
Expand Down
8 changes: 0 additions & 8 deletions src/testFolder/moreNesting/untestedFile.ts

This file was deleted.

4 changes: 3 additions & 1 deletion src/types/JsonFinal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ type JsonFinal = {

export type {
JsonFinal,
StatementCoverageReport
FileCoverageReport,
StatementCoverageReport,
StatementMap
};
47 changes: 47 additions & 0 deletions src/types/JsonFinalMockFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { JsonFinal, StatementCoverageReport, StatementMap } from './JsonFinal';

type LineConfig = { line: number, covered: boolean };
const createJsonFinalEntry = (fileName: string, lineConfigs: LineConfig[]): JsonFinal => {
const statementCoverageReport: StatementCoverageReport = {
statementMap: lineConfigs.reduce((obj, lineConfig) => ({
...obj,
[lineConfig.line - 1]: {
start: {
line: lineConfig.line,
column: 0
},
end: {
line: lineConfig.line,
column: 0
}
}
}), {}),
s: lineConfigs.reduce((obj, lineConfig) => ({
...obj,
[lineConfig.line - 1]: lineConfig.covered ? 1 : 0
}), {})
};

return {
[fileName]: {
path: fileName,
all: false,
...statementCoverageReport
}
}
}

const defaultJsonFinal: JsonFinal = {
...createJsonFinalEntry('src/exampleFile.ts', [{line: 1, covered: false}])
};


const createTestJsonFinal = (overwrites: Partial<JsonFinal> = {}): JsonFinal => ({
...defaultJsonFinal,
...overwrites
} as JsonFinal);

export {
createTestJsonFinal,
createJsonFinalEntry
};
Loading

0 comments on commit 4f2bca0

Please sign in to comment.