Skip to content

Commit fd3bd5e

Browse files
committed
chore(plugin-doc-coverage): add js files for integration test, remove unused dependency, new test
1 parent 79b6a02 commit fd3bd5e

File tree

14 files changed

+266
-167
lines changed

14 files changed

+266
-167
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function MyComponent() {
2+
return (
3+
<div>
4+
<h1>Hello World</h1>
5+
<p>This is a basic React component</p>
6+
</div>
7+
);
8+
}
9+
10+
export default MyComponent;

packages/plugin-doc-coverage/mocks/source-files-mock.generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
TypeAliasDeclaration,
99
VariableStatement,
1010
} from 'ts-morph';
11-
import type { CoverageType } from '../src/lib/runner/models';
11+
import type { CoverageType } from '../src/lib/runner/models.js';
1212

1313
export function sourceFileMock(
1414
file: string,

packages/plugin-doc-coverage/package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,5 @@
3838
"@code-pushup/models": "0.57.0",
3939
"zod": "^3.22.4",
4040
"ts-morph": "^24.0.0"
41-
},
42-
"peerDependenciesMeta": {
43-
"@nx/devkit": {
44-
"optional": true
45-
}
4641
}
4742
}

packages/plugin-doc-coverage/src/lib/config.unit.test.ts

Lines changed: 116 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,109 +4,135 @@ import {
44
docCoveragePluginConfigSchema,
55
} from './config.js';
66

7-
describe('docCoveragePluginConfigSchema', () => {
8-
it('accepts a valid configuration', () => {
9-
expect(() =>
10-
docCoveragePluginConfigSchema.parse({
11-
sourceGlob: ['src/**/*.ts'],
12-
onlyAudits: ['functions-coverage'],
13-
} satisfies DocCoveragePluginConfig),
14-
).not.toThrow();
15-
});
7+
describe('DocCoveragePlugin Configuration', () => {
8+
describe('docCoveragePluginConfigSchema', () => {
9+
it('accepts a valid configuration', () => {
10+
expect(() =>
11+
docCoveragePluginConfigSchema.parse({
12+
sourceGlob: ['src/**/*.ts'],
13+
onlyAudits: ['functions-coverage'],
14+
} satisfies DocCoveragePluginConfig),
15+
).not.toThrow();
16+
});
1617

17-
it('throws when skipAudits and onlyAudits are defined', () => {
18-
expect(() =>
19-
docCoveragePluginConfigSchema.parse({
20-
skipAudits: ['functions-coverage'],
21-
onlyAudits: ['classes-coverage'],
22-
}),
23-
).toThrow("You can't define 'skipAudits' and 'onlyAudits' simultaneously");
18+
it('throws when skipAudits and onlyAudits are defined', () => {
19+
expect(() =>
20+
docCoveragePluginConfigSchema.parse({
21+
skipAudits: ['functions-coverage'],
22+
onlyAudits: ['classes-coverage'],
23+
}),
24+
).toThrow(
25+
"You can't define 'skipAudits' and 'onlyAudits' simultaneously",
26+
);
27+
});
2428
});
25-
});
2629

27-
describe('sourceGlob', () => {
28-
it('accepts a valid source glob pattern', () => {
29-
expect(() =>
30-
docCoveragePluginConfigSchema.parse({
31-
sourceGlob: ['src/**/*.{ts,tsx}', '!**/*.spec.ts', '!**/*.test.ts'],
32-
} satisfies DocCoveragePluginConfig),
33-
).not.toThrow();
34-
});
30+
describe('sourceGlob', () => {
31+
it('accepts a valid source glob pattern', () => {
32+
expect(() =>
33+
docCoveragePluginConfigSchema.parse({
34+
sourceGlob: ['src/**/*.{ts,tsx}', '!**/*.spec.ts', '!**/*.test.ts'],
35+
} satisfies DocCoveragePluginConfig),
36+
).not.toThrow();
37+
});
3538

36-
it('uses default value for missing sourceGlob', () => {
37-
const result = docCoveragePluginConfigSchema.parse({});
38-
expect(result.sourceGlob).toEqual([
39-
'src/**/*.{ts,tsx}',
40-
'!**/*.spec.ts',
41-
'!**/*.test.ts',
42-
]);
43-
});
39+
it('uses default value for missing sourceGlob', () => {
40+
const result = docCoveragePluginConfigSchema.parse({});
41+
expect(result.sourceGlob).toEqual([
42+
'src/**/*.{ts,tsx}',
43+
'!**/*.spec.ts',
44+
'!**/*.test.ts',
45+
]);
46+
});
4447

45-
it('throws for invalid sourceGlob type', () => {
46-
expect(() =>
47-
docCoveragePluginConfigSchema.parse({
48-
sourceGlob: 123,
49-
}),
50-
).toThrow('Expected array');
48+
it('throws for invalid sourceGlob type', () => {
49+
expect(() =>
50+
docCoveragePluginConfigSchema.parse({
51+
sourceGlob: 123,
52+
}),
53+
).toThrow('Expected array');
54+
});
5155
});
52-
});
5356

54-
describe('onlyAudits', () => {
55-
it('accepts a valid `onlyAudits` array', () => {
56-
expect(() =>
57-
docCoveragePluginConfigSchema.parse({
58-
onlyAudits: ['functions-coverage', 'classes-coverage'],
59-
sourceGlob: ['src/**/*.ts'],
60-
}),
61-
).not.toThrow();
62-
});
57+
describe('onlyAudits', () => {
58+
it('accepts a valid `onlyAudits` array', () => {
59+
expect(() =>
60+
docCoveragePluginConfigSchema.parse({
61+
onlyAudits: ['functions-coverage', 'classes-coverage'],
62+
sourceGlob: ['src/**/*.ts'],
63+
}),
64+
).not.toThrow();
65+
});
6366

64-
it('accepts empty array for onlyAudits', () => {
65-
expect(() =>
66-
docCoveragePluginConfigSchema.parse({
67-
onlyAudits: [],
68-
sourceGlob: ['src/**/*.ts'],
69-
}),
70-
).not.toThrow();
71-
});
67+
it('accepts empty array for onlyAudits', () => {
68+
expect(() =>
69+
docCoveragePluginConfigSchema.parse({
70+
onlyAudits: [],
71+
sourceGlob: ['src/**/*.ts'],
72+
}),
73+
).not.toThrow();
74+
});
7275

73-
it('allows onlyAudits to be undefined', () => {
74-
const result = docCoveragePluginConfigSchema.parse({});
75-
expect(result.onlyAudits).toBeUndefined();
76-
});
76+
it('allows onlyAudits to be undefined', () => {
77+
const result = docCoveragePluginConfigSchema.parse({});
78+
expect(result.onlyAudits).toBeUndefined();
79+
});
7780

78-
it('throws for invalid onlyAudits type', () => {
79-
expect(() =>
80-
docCoveragePluginConfigSchema.parse({
81-
onlyAudits: 'functions-coverage',
82-
}),
83-
).toThrow('Expected array');
84-
});
81+
it('throws for invalid onlyAudits type', () => {
82+
expect(() =>
83+
docCoveragePluginConfigSchema.parse({
84+
onlyAudits: 'functions-coverage',
85+
}),
86+
).toThrow('Expected array');
87+
});
8588

86-
it('throws for array with non-string elements', () => {
87-
expect(() =>
88-
docCoveragePluginConfigSchema.parse({
89-
onlyAudits: [123, true],
90-
}),
91-
).toThrow('Expected string');
89+
it('throws for array with non-string elements', () => {
90+
expect(() =>
91+
docCoveragePluginConfigSchema.parse({
92+
onlyAudits: [123, true],
93+
}),
94+
).toThrow('Expected string');
95+
});
9296
});
93-
});
9497

95-
describe('skipAudits', () => {
96-
it('accepts valid audit slugs array', () => {
97-
expect(() =>
98-
docCoveragePluginConfigSchema.parse({
99-
skipAudits: ['functions-coverage', 'classes-coverage'],
100-
sourceGlob: ['src/**/*.ts'],
101-
}),
102-
).not.toThrow();
103-
});
98+
describe('skipAudits', () => {
99+
it('accepts valid audit slugs array', () => {
100+
expect(() =>
101+
docCoveragePluginConfigSchema.parse({
102+
skipAudits: ['functions-coverage', 'classes-coverage'],
103+
sourceGlob: ['src/**/*.ts'],
104+
}),
105+
).not.toThrow();
106+
});
107+
108+
it('accepts empty array for skipAudits', () => {
109+
expect(() =>
110+
docCoveragePluginConfigSchema.parse({
111+
skipAudits: [],
112+
sourceGlob: ['src/**/*.ts'],
113+
}),
114+
).not.toThrow();
115+
});
116+
117+
it('allows skipAudits to be undefined', () => {
118+
const result = docCoveragePluginConfigSchema.parse({});
119+
expect(result.skipAudits).toBeUndefined();
120+
});
121+
122+
it('throws for invalid skipAudits type', () => {
123+
expect(() =>
124+
docCoveragePluginConfigSchema.parse({
125+
skipAudits: 'functions-coverage',
126+
}),
127+
).toThrow('Expected array');
128+
});
104129

105-
it('throws for array with non-string elements', () => {
106-
expect(() =>
107-
docCoveragePluginConfigSchema.parse({
108-
skipAudits: [123, true],
109-
}),
110-
).toThrow('Expected string');
130+
it('throws for array with non-string elements', () => {
131+
expect(() =>
132+
docCoveragePluginConfigSchema.parse({
133+
skipAudits: [123, true],
134+
}),
135+
).toThrow('Expected string');
136+
});
111137
});
112138
});

packages/plugin-doc-coverage/src/lib/doc-coverage-plugin.unit.test.ts

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
import { describe, expect, it } from 'vitest';
2-
import type { RunnerConfig } from '@code-pushup/models';
3-
import { PLUGIN_SLUG } from './constants.js';
1+
import { describe, expect, it, vi } from 'vitest';
2+
import { PLUGIN_SLUG, groups } from './constants.js';
43
import {
54
PLUGIN_DESCRIPTION,
65
PLUGIN_DOCS_URL,
76
PLUGIN_TITLE,
87
docCoveragePlugin,
98
} from './doc-coverage-plugin.js';
9+
import { createRunnerFunction } from './runner/runner.js';
10+
import {
11+
filterAuditsByPluginConfig,
12+
filterGroupsByOnlyAudits,
13+
} from './utils.js';
14+
15+
vi.mock('./utils.js', () => ({
16+
filterAuditsByPluginConfig: vi.fn().mockReturnValue(['mockAudit']),
17+
filterGroupsByOnlyAudits: vi.fn().mockReturnValue(['mockGroup']),
18+
}));
1019

11-
vi.mock('./runner/index.ts', () => ({
12-
createRunnerConfig: vi.fn().mockReturnValue({
13-
command: 'node',
14-
outputFile: 'runner-output.json',
15-
} satisfies RunnerConfig),
20+
vi.mock('./runner/runner.js', () => ({
21+
createRunnerFunction: vi.fn().mockReturnValue(() => Promise.resolve([])),
1622
}));
1723

1824
describe('docCoveragePlugin', () => {
@@ -34,4 +40,34 @@ describe('docCoveragePlugin', () => {
3440
}),
3541
);
3642
});
43+
44+
it('should throw for invalid plugin options', async () => {
45+
await expect(
46+
docCoveragePlugin({
47+
// @ts-expect-error testing invalid config
48+
sourceGlob: 123,
49+
}),
50+
).rejects.toThrow('Expected array, received number');
51+
});
52+
53+
it('should filter groups', async () => {
54+
const config = { sourceGlob: ['src/**/*.ts'] };
55+
await docCoveragePlugin(config);
56+
57+
expect(filterGroupsByOnlyAudits).toHaveBeenCalledWith(groups, config);
58+
});
59+
60+
it('should filter audits', async () => {
61+
const config = { sourceGlob: ['src/**/*.ts'] };
62+
await docCoveragePlugin(config);
63+
64+
expect(filterAuditsByPluginConfig).toHaveBeenCalledWith(config);
65+
});
66+
67+
it('should forward options to runner function', async () => {
68+
const config = { sourceGlob: ['src/**/*.ts'] };
69+
await docCoveragePlugin(config);
70+
71+
expect(createRunnerFunction).toHaveBeenCalledWith(config);
72+
});
3773
});

packages/plugin-doc-coverage/src/lib/runner/__snapshots__/doc-processer.unit.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`getUnprocessedCoverageReport > should produce a full report 1`] = `
3+
exports[`getDocumentationReport > should produce a full report 1`] = `
44
{
55
"classes": {
66
"coverage": 33.33,

packages/plugin-doc-coverage/src/lib/runner/doc-processer.integration.test.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { processDocCoverage } from './doc-processer.js';
22

33
describe('processDocCoverage', () => {
4-
const sourcePath =
5-
'packages/plugin-doc-coverage/mocks/fixtures/angular/**/*.ts';
6-
74
it('should count total nodes from TypeScript files correctly', () => {
5+
const sourcePath =
6+
'packages/plugin-doc-coverage/mocks/fixtures/angular/**/*.ts';
87
const expectedNodeCount = 8;
98

109
const results = processDocCoverage({ sourceGlob: [sourcePath] });
@@ -17,7 +16,39 @@ describe('processDocCoverage', () => {
1716
expect(totalNodeCount).toBe(expectedNodeCount);
1817
});
1918

19+
it('should count total nodes from Javascript files correctly', () => {
20+
const sourcePath =
21+
'packages/plugin-doc-coverage/mocks/fixtures/react/**/*.js';
22+
const expectedNodeCount = 1;
23+
24+
const results = processDocCoverage({ sourceGlob: [sourcePath] });
25+
26+
const totalNodeCount = Object.values(results).reduce(
27+
(acc, node) => acc + node.nodesCount,
28+
0,
29+
);
30+
31+
expect(totalNodeCount).toBe(expectedNodeCount);
32+
});
33+
34+
it('should count total nodes from Javascript and TypeScript files correctly', () => {
35+
const sourcePath =
36+
'packages/plugin-doc-coverage/mocks/fixtures/**/*.{js,ts}';
37+
const expectedNodeCount = 9;
38+
39+
const results = processDocCoverage({ sourceGlob: [sourcePath] });
40+
41+
const totalNodeCount = Object.values(results).reduce(
42+
(acc, node) => acc + node.nodesCount,
43+
0,
44+
);
45+
46+
expect(totalNodeCount).toBe(expectedNodeCount);
47+
});
48+
2049
it('respect `sourceGlob` and only include matching files', () => {
50+
const sourcePath =
51+
'packages/plugin-doc-coverage/mocks/fixtures/angular/**/*.ts';
2152
const expectedNodeCount = 7;
2253

2354
const results = processDocCoverage({

0 commit comments

Comments
 (0)