Skip to content

Commit f0d6ad3

Browse files
committed
feat: hidden code from test files plot into html report
1 parent c37e92b commit f0d6ad3

File tree

7 files changed

+74
-34
lines changed

7 files changed

+74
-34
lines changed

cli/src/find-smells.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ async function execute() {
3939
const aggregator: SmellsList[] = [];
4040

4141
for (const file of pathWithAllFilesFound) {
42-
const fileContents = await fs.readFile(file, { encoding: 'utf8' });
43-
const smellDetector = new SmellDetector(fileContents, language);
42+
const fileContent = await fs.readFile(file, { encoding: 'utf8' });
43+
const smellDetector = new SmellDetector(fileContent, language);
4444
const smells = smellDetector.findAll().smells;
45-
aggregator.push({ fileName: file, smells, language });
45+
aggregator.push({ fileName: file, smells, language, fileContent });
4646
}
4747

4848
const to = path.resolve(reportOutput.replace('--report-output=', ''));

detector/src/reporters/SmellsAgreggator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class SmellsAggreagtor implements AgreggatorSmellls {
1616
await output.writeTo({
1717
totalSmells,
1818
data: this.totalTestFiles,
19-
averageSmellsPerTestFile: totalSmells / totalFiles
19+
averageSmellsPerTestFile: totalSmells / totalFiles,
2020
}, this.exportOptions);
2121
} catch (err) {
2222
console.log(err);

detector/src/reporters/layout/example.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<title>Test smells report</title>
66
<meta charset="UTF-8">
77
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
9+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css" />
10+
811
<script src="https://cdn.tailwindcss.com"></script>
912
<script>
1013
tailwind.config = {
@@ -84,13 +87,21 @@ <h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white"
8487
{{this.smells.length}}
8588
</td>
8689
</tr>
90+
<tr class="hidden">
91+
<td colspan="3" class="p-4" data-testid="{{@index}}-code">
92+
<pre><code>{{this.fileContent}}</code></pre>
93+
</td>
94+
</tr>
8795
{{/each}}
8896
</tbody>
8997
</table>
9098
</div>
9199

92100
</div>
93101

102+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
103+
<script>hljs.highlightAll();</script>
104+
94105
<script>
95106
var themeToggleDarkIcon = document.getElementById('theme-toggle-dark-icon');
96107
var themeToggleLightIcon = document.getElementById('theme-toggle-light-icon');

detector/src/reporters/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface SmellsList {
44
language: SupportedLanguages;
55
fileName: string;
66
smells: Smell[];
7+
fileContent: string;
78
}
89

910
export interface AgreggatorSmellls {

detector/test/html-report.integration.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ describe('html report', () => {
5858
{
5959
smells: [smell, smell, smell, smell],
6060
language: SupportedLanguages.javascript,
61-
fileName: 'first_test.js'
61+
fileName: 'first_test.js',
62+
fileContent: 'console.log("Hello world")',
6263
},
6364
];
6465

@@ -79,6 +80,14 @@ describe('html report', () => {
7980
expect(root.querySelector('table tbody tr')?.textContent).toContain('first_test.js');
8081
});
8182

83+
test('renders test file code', async () => {
84+
const aggregatedData = buildAggregatedData();
85+
const generatedHtml = await buildHtmlReportForTestSmellsFor(exportsOptions, filePath, aggregatedData);
86+
const root = parse(generatedHtml);
87+
88+
expect(root.querySelector('[data-testid="0-code"]')?.textContent).toContain('console.log("Hello world")');
89+
});
90+
8291
test('renders language', async () => {
8392
const generatedHtml = await buildHtmlReportForTestSmellsFor(exportsOptions, filePath, buildAggregatedData());
8493
const root = parse(generatedHtml);

detector/test/smells-aggregator.test.ts

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,29 @@ import { Smell, SmellType, SupportedLanguages } from '../src/types';
77

88
vi.mock('../src/reporters/Output');
99

10+
function buildListWithSingleSmell(smells: Smell[]): SmellsList[] {
11+
return [
12+
{
13+
smells,
14+
language: SupportedLanguages.javascript,
15+
fileName: 'random',
16+
fileContent: 'console.log("Hello world")',
17+
},
18+
];
19+
}
20+
21+
function createSingleSmell(): Smell {
22+
return {
23+
type: SmellType.consoleStatement,
24+
lineStart: 0,
25+
lineEnd: 1,
26+
startAt: 10,
27+
endsAt: 20,
28+
description: '',
29+
diagnostic: ''
30+
};
31+
}
32+
1033
describe('smells aggregator', () => {
1134
test('no smells for a single file', async () => {
1235
const smellsFound: SmellsList[] = [];
@@ -22,24 +45,21 @@ describe('smells aggregator', () => {
2245
expect(write.mock.calls[0][1]).toEqual(exportsOptions);
2346
});
2447

25-
test('match detected smells found to write in the output', async () => {
26-
const smell: Smell = {
27-
type: SmellType.consoleStatement,
28-
lineStart: 0,
29-
lineEnd: 1,
30-
startAt: 10,
31-
endsAt: 20,
32-
description: '',
33-
diagnostic: ''
34-
};
48+
test('should send file contents to the output', async () => {
49+
const smellsFound: SmellsList[] = buildListWithSingleSmell([createSingleSmell()]);
50+
const exportsOptions: ExportOptions = { to: '.' };
3551

36-
const smellsFound: SmellsList[] = [
37-
{
38-
smells: [smell],
39-
language: SupportedLanguages.javascript,
40-
fileName: 'random'
41-
},
42-
];
52+
const write = vi.mocked(HtmlOutput.prototype.writeTo = vi.fn());
53+
54+
const reporter = new SmellsAggreagtor(smellsFound, exportsOptions);
55+
56+
await reporter.build();
57+
58+
expect(write.mock.calls[0][0].data[0].fileContent).toEqual('console.log("Hello world")');
59+
});
60+
61+
test('match detected smells found to write in the output', async () => {
62+
const smellsFound: SmellsList[] = buildListWithSingleSmell([createSingleSmell()]);
4363
const exportsOptions: ExportOptions = { to: '.' };
4464

4565
const write = vi.mocked(HtmlOutput.prototype.writeTo = vi.fn());
@@ -62,13 +82,7 @@ describe('smells aggregator', () => {
6282
diagnostic: ''
6383
};
6484

65-
const smellsFound: SmellsList[] = [
66-
{
67-
smells: [smell],
68-
language: SupportedLanguages.javascript,
69-
fileName: 'random'
70-
},
71-
];
85+
const smellsFound: SmellsList[] = buildListWithSingleSmell([createSingleSmell()]);
7286
const exportsOptions: ExportOptions = { to: '.' };
7387

7488
const write = vi.mocked(HtmlOutput.prototype.writeTo = vi.fn());
@@ -86,12 +100,14 @@ describe('smells aggregator', () => {
86100
{
87101
smells: [smell, smell, smell, smell, smell],
88102
language: SupportedLanguages.javascript,
89-
fileName: 'first_test.js'
103+
fileName: 'first_test.js',
104+
fileContent: 'console.log("Hello world")',
90105
},
91106
{
92107
smells: [smell, smell, smell, smell, smell],
93108
language: SupportedLanguages.javascript,
94-
fileName: 'second_test.js'
109+
fileName: 'second_test.js',
110+
fileContent: 'var content = 0',
95111
},
96112
];
97113
const exportsOptions: ExportOptions = { to: '.' };
@@ -111,12 +127,14 @@ describe('smells aggregator', () => {
111127
{
112128
smells: [smell, smell, smell, smell],
113129
language: SupportedLanguages.javascript,
114-
fileName: 'first_test.js'
130+
fileName: 'first_test.js',
131+
fileContent: 'console.log("Hello world")',
115132
},
116133
{
117134
smells: [],
118135
language: SupportedLanguages.javascript,
119-
fileName: 'second_test.js'
136+
fileName: 'second_test.js',
137+
fileContent: 'console.log("Hello world")',
120138
},
121139
];
122140
const exportsOptions: ExportOptions = { to: '.' };
@@ -129,4 +147,4 @@ describe('smells aggregator', () => {
129147

130148
expect(write.mock.calls[0][0].averageSmellsPerTestFile).toEqual(2);
131149
});
132-
});
150+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@vitest/coverage-v8": "^2.1.6"
1515
},
1616
"scripts": {
17+
"build": "npm exec --workspaces -- npm run compile",
1718
"test": "npm exec --workspaces -- npm run test",
1819
"clean": "npm exec --workspaces -- rimraf node_modules && rimraf node_modules"
1920
},

0 commit comments

Comments
 (0)