Skip to content

Commit c195dab

Browse files
committed
refactor: fix tests
1 parent b5bef63 commit c195dab

File tree

16 files changed

+347
-303
lines changed

16 files changed

+347
-303
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "reporter-test-package",
3+
"dependencies": {
4+
"zod": "^3.23.6"
5+
}
6+
}

e2e/plugin-knip-e2e/tests/collect.e2e.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('PLUGIN collect report with knip-plugin NPM package', () => {
4747
const report = await readJsonFile(
4848
path.join(defaultSetupDir, '.code-pushup', 'report.json'),
4949
);
50-
expect(() => reportSchema.parse(report)).not.toThrow();
50+
expect(() => reportSchema.parse(report)).not.toThrowError();
5151
expect(
5252
omitVariableReportData(report as Report, { omitAuditData: true }),
5353
).toMatchSnapshot();
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { cp } from 'node:fs/promises';
2+
import path from 'node:path';
3+
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
4+
import { auditOutputsSchema } from '@code-pushup/models';
5+
import { nxTargetProject } from '@code-pushup/test-nx-utils';
6+
import {
7+
E2E_ENVIRONMENTS_DIR,
8+
TEST_OUTPUT_DIR,
9+
restoreNxIgnoredFiles,
10+
teardownTestFolder,
11+
} from '@code-pushup/test-utils';
12+
import { executeProcess, readJsonFile } from '@code-pushup/utils';
13+
14+
describe('knip reporter', () => {
15+
const testFileDir = path.join(
16+
E2E_ENVIRONMENTS_DIR,
17+
nxTargetProject(),
18+
TEST_OUTPUT_DIR,
19+
'reporter',
20+
);
21+
const reporterSetupDir = path.join(testFileDir, 'reporter-setup');
22+
const fixturesDir = path.join('e2e', nxTargetProject(), 'mocks/fixtures');
23+
24+
beforeAll(async () => {
25+
await cp(fixturesDir, testFileDir, { recursive: true });
26+
await restoreNxIgnoredFiles(testFileDir);
27+
});
28+
29+
afterAll(async () => {
30+
await teardownTestFolder(testFileDir);
31+
});
32+
33+
it('should execute knip with custom reporter and generate report', async () => {
34+
const reporterPath = path.join(
35+
reporterSetupDir,
36+
'node_modules',
37+
'@code-pushup',
38+
'plugin-knip',
39+
'src',
40+
'lib',
41+
'reporter.js',
42+
);
43+
const outputFile = path.join(reporterSetupDir, 'knip-report.json');
44+
const customReporterOptions = JSON.stringify({
45+
outputFile: 'knip-report.json',
46+
});
47+
48+
const { code } = await executeProcess({
49+
command: 'npx',
50+
args: [
51+
'knip',
52+
'--no-exit-code',
53+
`--reporter=${reporterPath}`,
54+
`--reporter-options=${customReporterOptions}`,
55+
],
56+
cwd: reporterSetupDir,
57+
});
58+
59+
expect(code).toBe(0);
60+
61+
const report = await readJsonFile(outputFile);
62+
expect(() => auditOutputsSchema.parse(report)).not.toThrowError();
63+
expect(report).toEqual(
64+
expect.arrayContaining([
65+
expect.objectContaining({
66+
slug: 'unused-dependencies',
67+
score: 0,
68+
value: 1,
69+
details: {
70+
issues: [
71+
{
72+
message: 'Unused dependency zod',
73+
severity: 'error',
74+
source: {
75+
file: 'package.json',
76+
},
77+
},
78+
],
79+
},
80+
}),
81+
]),
82+
);
83+
});
84+
});

package-lock.json

Lines changed: 88 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"devDependencies": {
88
"@code-pushup/eslint-config": "^0.14.8",
99
"@code-pushup/models": "^0.92.1",
10+
"@nx/eslint-plugin": "^22.1.3",
1011
"@nx/vite": "21.6.10",
1112
"@nx/vitest": "^22.1.3",
1213
"@nx/web": "22.1.3",

packages/plugin-knip/project.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
"sourceRoot": "packages/plugin-knip/src",
55
"projectType": "library",
66
"targets": {
7-
"build": {},
7+
"build": {
8+
"options": {
9+
"additionalEntryPoints": ["packages/plugin-knip/src/lib/reporter.ts"]
10+
}
11+
},
812
"lint": {},
913
"unit-test": {},
1014
"int-test": {}

0 commit comments

Comments
 (0)