Skip to content

Commit 84b8c28

Browse files
authored
feat(utils): implement a new design for stdout (#206)
Closes #190
1 parent e778553 commit 84b8c28

File tree

12 files changed

+221
-132
lines changed

12 files changed

+221
-132
lines changed

e2e/cli-e2e/tests/collect.spec.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { cleanFolderPutGitKeep } from '../mocks/fs.mock';
66

77
describe('CLI collect', () => {
88
const exampleCategoryTitle = 'Code style';
9-
const exampleAuditTitle =
10-
'Require `const` declarations for variables that are never reassigned after declared';
9+
const exampleAuditTitle = 'Require `const` declarations for variables';
1110

1211
const omitVariableData = ({
1312
date,

package-lock.json

+24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@swc/helpers": "~0.5.0",
1616
"bundle-require": "^4.0.1",
1717
"chalk": "^5.3.0",
18+
"cli-table3": "^0.6.3",
1819
"cliui": "^8.0.1",
1920
"multi-progress-bars": "^5.0.3",
2021
"simple-git": "^3.20.0",

packages/core/src/lib/implementation/persist.spec.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import {
1313
CODE_PUSHUP_DOMAIN,
1414
FOOTER_PREFIX,
15+
NEW_LINE,
1516
README_LINK,
1617
} from '@code-pushup/utils';
1718
import { mockConsole, unmockConsole } from '../../../test';
@@ -76,7 +77,9 @@ describe('persistReport', () => {
7677

7778
it('should stdout as format by default`', async () => {
7879
await persistReport(dummyReport, dummyConfig);
79-
expect(logs).toContain(`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`);
80+
expect(logs.join(NEW_LINE)).toContain(
81+
`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`,
82+
);
8083

8184
expect(() => readReport('json')).not.toThrow();
8285
expect(() => readReport('md')).toThrow('no such file or directory');
@@ -89,7 +92,9 @@ describe('persistReport', () => {
8992
...dummyConfig,
9093
persist,
9194
});
92-
expect(logs).toContain(`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`);
95+
expect(logs.join(NEW_LINE)).toContain(
96+
`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`,
97+
);
9398

9499
expect(() => readReport('json')).not.toThrow('no such file or directory');
95100
expect(() => readReport('md')).toThrow('no such file or directory');
@@ -143,7 +148,9 @@ describe('persistReport', () => {
143148
`${FOOTER_PREFIX} [Code PushUp](${README_LINK})`,
144149
);
145150

146-
expect(logs).toContain(`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`);
151+
expect(logs.join(NEW_LINE)).toContain(
152+
`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`,
153+
);
147154
});
148155

149156
it('should persist some formats`', async () => {
@@ -162,7 +169,9 @@ describe('persistReport', () => {
162169
`${FOOTER_PREFIX} [Code PushUp](${README_LINK})`,
163170
);
164171

165-
expect(logs).toContain(`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`);
172+
expect(logs.join(NEW_LINE)).toMatch(
173+
`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`,
174+
);
166175
});
167176

168177
// @TODO: should throw PersistDirError

packages/core/src/lib/implementation/persist.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function persistReport(
3535
let scoredReport;
3636
if (format.includes('stdout')) {
3737
scoredReport = scoreReport(report);
38-
reportToStdout(scoredReport);
38+
console.log(reportToStdout(scoredReport));
3939
}
4040

4141
// collect physical format outputs

packages/utils/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"chalk": "^5.3.0",
88
"cliui": "^8.0.1",
99
"simple-git": "^3.20.0",
10-
"multi-progress-bars": "^5.0.3"
10+
"multi-progress-bars": "^5.0.3",
11+
"cli-table3": "^0.6.3"
1112
}
1213
}

packages/utils/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ export {
4242
distinct,
4343
slugify,
4444
} from './lib/transformation';
45+
export { NEW_LINE } from './lib/md';

packages/utils/src/lib/__snapshots__/report-to-md.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,5 +436,5 @@ The following plugins were run:
436436
|ESLint|47|\`0.1.0\`|368 ms|
437437
|Lighthouse|5|\`0.1.0\`|1.23 s|
438438
439-
Made with ❤ by [Code PushUp](https://github.com/flowup/quality-metrics-cli#readme)"
439+
Made with ❤ by [Code PushUp](https://github.com/flowup/quality-metrics-cli#readme)"
440440
`;
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,90 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`report-to-stdout > should contain all sections when using the fixture report 1`] = `
4-
"Code PushUp Report - @code-pushup/[email protected]
5-
---
6-
Package Name: @code-pushup/core
7-
Version: 0.1.0
8-
Commit: feat(cli): add logic for markdown report - 7eba125ad5643c2f90cb21389fc3442d786f43f9
9-
Date: today
10-
Duration: 42ms
11-
Plugins: 1
12-
Audits: 1
13-
---
4+
"Code PushUp Report - @code-pushup/[email protected]
145
156
16-
🏷 Category ⭐ Score 🛡 Audits
17-
Category 1 0 1/1
7+
ESLint audits
188
9+
● Disallow assignment operators in conditional expressions 0
10+
● Disallow reassigning \`const\` variables 0
11+
● Disallow the use of \`debugger\` 0
12+
● Disallow invalid regular expression strings in \`RegExp\` construct 0
13+
ors
14+
● Disallow the use of undeclared variables unless mentioned in \`/*g 0
15+
lobal */\` comments
16+
● Disallow loops with a body that allows only one iteration 0
17+
● Disallow negating the left operand of relational operators 0
18+
● Disallow use of optional chaining in contexts where the \`undefine 0
19+
d\` value is not allowed
20+
● Disallow unused variables 1 warning
21+
● Require calls to \`isNaN()\` when checking for \`NaN\` 0
22+
● Enforce comparing \`typeof\` expressions against valid strings 0
23+
● Require braces around arrow function bodies 1 warning
24+
● Enforce camelcase naming convention 0
25+
● Enforce consistent brace style for all control statements 0
26+
● Require the use of \`===\` and \`!==\` 1 warning
27+
● Enforce a maximum number of lines of code in a function 1 warning
28+
● Enforce a maximum number of lines per file 0
29+
● Disallow variable declarations from shadowing variables declared 3 warnings
30+
in the outer scope
31+
● Require \`let\` or \`const\` instead of \`var\` 0
32+
● Require or disallow method and property shorthand syntax for obje 3 warnings
33+
ct literals
34+
● Require using arrow functions for callbacks 0
35+
● Require \`const\` declarations for variables that are never reassig 1 warning
36+
ned after declared
37+
● Disallow using Object.assign with an object literal as the first 0
38+
argument and prefer the use of object spread instead
39+
● Require or disallow \\"Yoda\\" conditions 0
40+
● Disallow missing \`key\` props in iterators/collection literals 1 warning
41+
● Disallow missing props validation in a React component definition 6 warnings
42+
● Disallow missing React when using JSX 0
43+
● enforces the Rules of Hooks 0
44+
● verifies the list of dependencies for Hooks like useEffect and si 2 warnings
45+
milar
46+
● Disallow missing displayName in a React component definition 0
47+
● Disallow comments from being inserted as text nodes 0
48+
● Disallow duplicate properties in JSX 0
49+
● Disallow \`target=\\"_blank\\"\` attribute without \`rel=\\"noreferrer\\"\` 0
50+
● Disallow undeclared variables in JSX 0
51+
● Disallow React to be incorrectly marked as unused 0
52+
● Disallow variables used in JSX to be incorrectly marked as unused 0
53+
● Disallow passing of children as props 0
54+
● Disallow when a DOM element is using both children and dangerousl 0
55+
ySetInnerHTML
56+
● Disallow usage of deprecated methods 0
57+
● Disallow direct mutation of this.state 0
58+
● Disallow usage of findDOMNode 0
59+
● Disallow usage of isMounted 0
60+
● Disallow usage of the return value of ReactDOM.render 0
61+
● Disallow using string references 0
62+
● Disallow unescaped HTML entities from appearing in markup 0
63+
● Disallow usage of unknown DOM property 0
64+
● Enforce ES5 or ES6 class for returning value in render function 0
1965
20-
Category 1 0
21-
- Audit Title (1)
22-
audit description
23-
http://www.my-docs.dev
2466
67+
Lighthouse audits
2568
69+
● First Contentful Paint 1.2 s
70+
● Largest Contentful Paint 1.5 s
71+
● Total Blocking Time 0 ms
72+
● Cumulative Layout Shift 0
73+
● Speed Index 1.2 s
2674
27-
Made with ❤️ by code-pushup.dev"
75+
76+
Categories
77+
78+
┌────────────────┬───────┬────────┐
79+
│ Category │ Score │ Audits │
80+
├────────────────┼───────┼────────┤
81+
│ Performance │ 92 │ 6 │
82+
├────────────────┼───────┼────────┤
83+
│ Bug prevention │ 68 │ 16 │
84+
├────────────────┼───────┼────────┤
85+
│ Code style │ 54 │ 13 │
86+
└────────────────┴───────┴────────┘
87+
88+
Made with ❤ by code-pushup.dev
89+
"
2890
`;
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1-
import { afterEach, beforeEach, describe } from 'vitest';
2-
import { minimalReport } from '@code-pushup/models/testing';
3-
import { mockConsole, unmockConsole } from '../../test';
1+
import { describe } from 'vitest';
2+
import { report } from '@code-pushup/models/testing';
43
import { reportToStdout } from './report-to-stdout';
54
import { scoreReport } from './scoring';
65

7-
let logs: string[];
8-
96
describe('report-to-stdout', () => {
10-
beforeEach(async () => {
11-
logs = [];
12-
mockConsole(msg => logs.push(msg));
13-
});
14-
afterEach(() => {
15-
unmockConsole();
16-
});
17-
187
it('should contain all sections when using the fixture report', () => {
19-
reportToStdout(scoreReport(minimalReport('tmp')));
20-
const logOutput = logs.join('\n');
21-
expect(logOutput).toMatchSnapshot();
8+
const logOutput = reportToStdout(scoreReport(report()));
9+
// logOutput.replace(/\u001B\[\d+m/g, '') removes all color codes from the output
10+
// for snapshot readability
11+
// eslint-disable-next-line no-control-regex
12+
expect(logOutput.replace(/\u001B\[\d+m/g, '')).toMatchSnapshot();
2213
});
2314
});

0 commit comments

Comments
 (0)