|
| 1 | +import { test, describe, expect, afterEach, beforeEach } from 'vitest'; |
| 2 | +import { exec } from "child_process"; |
| 3 | +import { promisify } from "util"; |
| 4 | +import { parse } from 'node-html-parser'; |
| 5 | +import { SmellsBuilder } from '../src/smells-builder'; |
| 6 | +import { SmellsAggreagtor } from '../src/reporters/SmellsAgreggator'; |
| 7 | +import { AggregatedData, ExportOptions, SmellsList } from '../src/reporters/types'; |
| 8 | +import { Smell, SmellType, SupportedLanguages } from '../src/types'; |
| 9 | +import { HtmlOutput } from '../src/reporters/Output'; |
| 10 | +import { readFileSync, rmSync } from 'fs'; |
| 11 | + |
| 12 | +describe('html report', () => { |
| 13 | + const exportsOptions: ExportOptions = { to: '.' }; |
| 14 | + const filePath = `${exportsOptions.to}/smelly-report.html`; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + rmSync(filePath, { force: true }); |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + rmSync(filePath, { force: true }); |
| 22 | + }); |
| 23 | + |
| 24 | + test('renders empty table when no tests are found', async () => { |
| 25 | + const generatedHtml = await buildHtmlReportForTestSmells(exportsOptions, filePath); |
| 26 | + const root = parse(generatedHtml); |
| 27 | + |
| 28 | + expect(root.querySelectorAll('table tbody tr').length).toEqual(0); |
| 29 | + }); |
| 30 | + |
| 31 | + test('renders report title', async () => { |
| 32 | + const generatedHtml = await buildHtmlReportForTestSmells(exportsOptions, filePath); |
| 33 | + const root = parse(generatedHtml); |
| 34 | + |
| 35 | + expect(root.querySelector('[data-testid="report-title"]')?.textContent).toEqual('Test smells report'); |
| 36 | + }); |
| 37 | + |
| 38 | + test('renders total number of smells found', async () => { |
| 39 | + const generatedHtml = await buildHtmlReportForTestSmells(exportsOptions, filePath); |
| 40 | + const root = parse(generatedHtml); |
| 41 | + |
| 42 | + expect(root.querySelector('[data-testid="total-smells-found"]')?.textContent).toEqual('0'); |
| 43 | + expect(root.querySelector('[data-testid="title-smells-found"]')?.textContent).toEqual('Test smells'); |
| 44 | + }); |
| 45 | + |
| 46 | + test('renders the number of test files', async () => { |
| 47 | + const generatedHtml = await buildHtmlReportForTestSmells(exportsOptions, filePath); |
| 48 | + const root = parse(generatedHtml); |
| 49 | + |
| 50 | + expect(root.querySelector('[data-testid="total-test-files"]')?.textContent).toEqual('0'); |
| 51 | + expect(root.querySelector('[data-testid="title-test-files"]')?.textContent).toEqual('Test files'); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +async function buildHtmlReportForTestSmells(exportsOptions: ExportOptions, filePath: string) { |
| 56 | + const smellsFound: SmellsList[] = []; |
| 57 | + const aggregatedData: AggregatedData = { data: smellsFound, totalSmells: 0, averageSmellsPerTestFile: 0 }; |
| 58 | + |
| 59 | + const reporter = new HtmlOutput(); |
| 60 | + await reporter.writeTo(aggregatedData, exportsOptions); |
| 61 | + |
| 62 | + const generatedHtml = readFileSync(filePath, { encoding: 'utf8', flag: 'r' }); |
| 63 | + return generatedHtml; |
| 64 | +} |
0 commit comments