-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Adds tests for FileCoverageHtml, creates mockFactories and refa…
…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
1 parent
12c6cfe
commit 4f2bca0
Showing
11 changed files
with
259 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,3 +103,4 @@ typings/ | |
.tern-port | ||
|
||
!test/mockReports/coverage | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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%'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
Oops, something went wrong.