Skip to content

Commit c4eb76b

Browse files
authored
test: add tests (#463)
1 parent a506892 commit c4eb76b

13 files changed

Lines changed: 86 additions & 65 deletions

File tree

examples/plugins/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ export { create as packageJsonPlugin } from './package-json/src/package-json.plu
1414
export {
1515
create as lighthousePlugin,
1616
LIGHTHOUSE_OUTPUT_FILE_DEFAULT,
17-
corePerfGroupRefs as lighthouseCorePerfGroupRefs,
17+
recommendedRefs as lighthouseCorePerfGroupRefs,
1818
} from './lighthouse/src/index';

examples/plugins/src/lighthouse/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
# lighthouse-plugin
1+
# lighthouse-plugin example
22

33
🕵️ **Code PushUp plugin for Lighthouse reports** 🔥
44

55
---
66

7+
> [!NOTE]
8+
> The real implementation of lighthouse lives [here](../../../../packages/plugin-lighthouse)
9+
710
<img alt="Code PushUp plugin for lighthouse reports" src="./docs/images/lighthouse-plugin-cover.png" height="655">
811

912
The plugin analyzes a given URL and creates Lighthouse audits.
@@ -43,9 +46,7 @@ You can configure the plugin with the following options:
4346
};
4447
```
4548

46-
4. (Optional) Reference audits (or groups) that you wish to include in custom categories (use `npx code-pushup print-config` to list audits and groups).
47-
48-
Assign weights based on what influence each audit and group should have on the overall category score (assign weight 0 to only include it for extra info, without influencing the category score).
49+
4. (Optional) Set up categories (use `npx code-pushup print-config` to list audits and groups).
4950

5051
```js
5152
import fileSizePlugin, { recommendedRefs as lighthouseRecommendedRefs } from './lighthouse.plugin';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export { create } from './lighthouse.plugin';
22
export {
33
LIGHTHOUSE_OUTPUT_FILE_DEFAULT,
4-
corePerfGroupRefs,
4+
corePerfGroupRefs as recommendedRefs,
55
PLUGIN_SLUG,
66
audits,
77
} from './constants';

examples/plugins/src/lighthouse/src/lighthouse.plugin.integration.test.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,7 @@ import { LIGHTHOUSE_OUTPUT_FILE_DEFAULT, corePerfGroupRefs } from './constants';
1313
import { audits, PLUGIN_SLUG as slug } from './index';
1414
import { create } from './lighthouse.plugin';
1515

16-
describe('lighthouse-create-export', () => {
17-
beforeEach(() => {
18-
vol.fromJSON(
19-
{
20-
[LIGHTHOUSE_OUTPUT_FILE_DEFAULT]: JSON.stringify(lhr),
21-
},
22-
MEMFS_VOLUME,
23-
);
24-
});
25-
16+
describe('lighthouse-create-export-config', () => {
2617
it('should return valid PluginConfig if create is called', async () => {
2718
const pluginConfig = await create({ url: LIGHTHOUSE_URL });
2819
expect(() => pluginConfigSchema.parse(pluginConfig)).not.toThrow();
@@ -37,7 +28,7 @@ describe('lighthouse-create-export', () => {
3728
});
3829
});
3930

40-
it('should parse options for defaults correctly', async () => {
31+
it('should parse options for defaults correctly in runner args', async () => {
4132
const pluginConfig = await create({
4233
url: 'https://code-pushup.com',
4334
});
@@ -58,7 +49,7 @@ describe('lighthouse-create-export', () => {
5849
]);
5950
});
6051

61-
it('should parse options for headless by default to "new"', async () => {
52+
it('should parse options for headless by default to "new" in runner args', async () => {
6253
const pluginConfig = await create({
6354
url: LIGHTHOUSE_URL,
6455
});
@@ -67,7 +58,7 @@ describe('lighthouse-create-export', () => {
6758
);
6859
});
6960

70-
it('should parse options for headless to new if true is given', async () => {
61+
it('should parse options for headless to new if true is given in runner args', async () => {
7162
const pluginConfig = await create({
7263
url: LIGHTHOUSE_URL,
7364
headless: true,
@@ -77,7 +68,7 @@ describe('lighthouse-create-export', () => {
7768
);
7869
});
7970

80-
it('should parse options for headless to new if false is given', async () => {
71+
it('should parse options for headless to new if false is given in runner args', async () => {
8172
const pluginConfig = await create({
8273
url: LIGHTHOUSE_URL,
8374
headless: false,
@@ -87,7 +78,7 @@ describe('lighthouse-create-export', () => {
8778
);
8879
});
8980

90-
it('should parse options for userDataDir correctly', async () => {
81+
it('should override userDataDir option when given in runner args', async () => {
9182
const pluginConfig = await create({
9283
url: LIGHTHOUSE_URL,
9384
userDataDir: 'test',
@@ -98,7 +89,17 @@ describe('lighthouse-create-export', () => {
9889
]),
9990
);
10091
});
92+
});
10193

94+
describe('lighthouse-create-export-execution', () => {
95+
beforeEach(() => {
96+
vol.fromJSON(
97+
{
98+
[LIGHTHOUSE_OUTPUT_FILE_DEFAULT]: JSON.stringify(lhr),
99+
},
100+
MEMFS_VOLUME,
101+
);
102+
});
102103
it('should return PluginConfig that executes correctly', async () => {
103104
const pluginConfig = await create({ url: LIGHTHOUSE_URL });
104105
await expect(executePlugin(pluginConfig)).resolves.toMatchObject(
@@ -125,19 +126,23 @@ describe('lighthouse-create-export', () => {
125126
const { audits: auditOutputs } = await executePlugin(pluginConfig);
126127

127128
expect(auditOutputs).toHaveLength(1);
129+
expect(auditOutputs[0]?.slug).toBe('largest-contentful-paint');
128130
});
129131
}, 30_000);
130132

131133
describe('lighthouse-audits-export', () => {
132-
it.each(audits)('should be a valid audit meta info', audit => {
133-
expect(() => auditSchema.parse(audit)).not.toThrow();
134-
});
134+
it.each(audits.map(a => [a.slug, a]))(
135+
'should have a valid audit meta info for %s',
136+
(_, audit) => {
137+
expect(() => auditSchema.parse(audit)).not.toThrow();
138+
},
139+
);
135140
});
136141

137142
describe('lighthouse-corePerfGroupRefs-export', () => {
138-
it.each(corePerfGroupRefs)(
139-
'should be a valid category reference',
140-
categoryRef => {
143+
it.each(corePerfGroupRefs.map(g => [g.slug, g]))(
144+
'should be a valid category reference for %s',
145+
(_, categoryRef) => {
141146
expect(() => categoryRefSchema.parse(categoryRef)).not.toThrow();
142147
},
143148
);

examples/plugins/src/lighthouse/src/lighthouse.plugin.unit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('lighthouse-runnerConfig', () => {
2525
);
2626
});
2727

28-
it('should execute if with output options', () => {
28+
it('should execute with output "json" and output-path "lighthouse-report.json" by default', () => {
2929
expect(runnerConfig(baseOptions)).toEqual(
3030
expect.objectContaining({
3131
args: expect.arrayContaining([

examples/plugins/src/lighthouse/src/utils.unit.test.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, expect, it } from 'vitest';
22
import { LIGHTHOUSE_URL } from '../mock/constants';
3-
import { create } from './lighthouse.plugin';
43
import {
54
AuditsNotImplementedError,
65
WithSlug,
@@ -79,35 +78,33 @@ describe('getLighthouseCliArguments', () => {
7978
).toEqual(expect.arrayContaining(['https://code-pushup-portal.com']));
8079
});
8180

82-
it('should parse options for headless to new if true is given', async () => {
83-
const pluginConfig = await create({
81+
it('should parse options for headless to new if true is given', () => {
82+
const args = getLighthouseCliArguments({
8483
url: LIGHTHOUSE_URL,
85-
headless: true,
84+
headless: 'new',
8685
});
87-
expect(pluginConfig.runner.args).toEqual(
86+
expect(args).toEqual(
8887
expect.arrayContaining(['--chrome-flags="--headless=new"']),
8988
);
9089
});
9190

92-
it('should parse options for headless to new if false is given', async () => {
93-
const pluginConfig = await create({
91+
it('should not include options for headless if false is given', () => {
92+
const args = getLighthouseCliArguments({
9493
url: LIGHTHOUSE_URL,
9594
headless: false,
9695
});
97-
expect(pluginConfig.runner.args).toEqual(
96+
expect(args).toEqual(
9897
expect.not.arrayContaining(['--chrome-flags="--headless=new"']),
9998
);
10099
});
101100

102-
it('should parse options for userDataDir correctly', async () => {
103-
const pluginConfig = await create({
101+
it('should use userDataDir option in chrome flags when given', () => {
102+
const args = getLighthouseCliArguments({
104103
url: LIGHTHOUSE_URL,
105104
userDataDir: 'test',
106105
});
107-
expect(pluginConfig.runner.args).toEqual(
108-
expect.arrayContaining([
109-
'--chrome-flags="--headless=new --user-data-dir=test"',
110-
]),
106+
expect(args).toEqual(
107+
expect.arrayContaining(['--chrome-flags="--user-data-dir=test"']),
111108
);
112109
});
113110
});

packages/cli/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Each example is fully tested to demonstrate best practices for plugin testing as
156156
157157
- 📏 [File Size](../../examples/plugins/src/file-size)
158158
- 📦 [Package Json](../../examples/plugins/src/package-json)
159-
- 🔥 [Lighthouse](../../examples/plugins/src/lighthouse)
159+
- 🔥 [Lighthouse](../../examples/plugins/src/lighthouse) (official implementation [here](../../../../packages/plugin-lighthouse))
160160
161161
## CLI commands and options
162162

packages/utils/perf/score-report/optimized1.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// Note: The plugins of the ScoredReport are not structured correctly, hence the ESLint disables.
33
import { Report } from '@code-pushup/models';
44
import { ScoredReport } from '../../src';
5-
import { ScoredCategoryConfig } from '../../src/lib/reports/scoring';
5+
import {
6+
GroupRefInvalidError,
7+
ScoredCategoryConfig,
8+
} from '../../src/lib/reports/scoring';
69

710
export function calculateScore<T extends { weight: number }>(
811
refs: T[],
@@ -37,9 +40,7 @@ export function scoreReportOptimized1(report: Report): ScoredReport {
3740
`${slug}-${ref.slug}-audit`,
3841
)?.score;
3942
if (score == null) {
40-
throw new Error(
41-
`Group has invalid ref - audit with slug ${slug}-${ref.slug}-audit not found`,
42-
);
43+
throw new GroupRefInvalidError(ref.slug, slug);
4344
}
4445
return score;
4546
}),

packages/utils/perf/score-report/optimized2.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// Note: The plugins of the ScoredReport are not structured correctly, hence the ESLint disables.
33
import { CategoryRef, GroupRef, Report } from '@code-pushup/models';
44
import { ScoredReport } from '../../src';
5-
import { ScoredCategoryConfig } from '../../src/lib/reports/scoring';
5+
import {
6+
GroupRefInvalidError,
7+
ScoredCategoryConfig,
8+
} from '../../src/lib/reports/scoring';
69

710
export function calculateScore<T extends { weight: number }>(
811
refs: T[],
@@ -34,9 +37,7 @@ export function scoreReportOptimized2(report: Report): ScoredReport {
3437
`${slug}-${ref.slug}-audit`,
3538
)?.score;
3639
if (score == null) {
37-
throw new Error(
38-
`Group has invalid ref - audit with slug ${slug}-${ref.slug}-audit not found`,
39-
);
40+
throw new GroupRefInvalidError(ref.slug, slug);
4041
}
4142
return score;
4243
}

packages/utils/perf/score-report/optimized3.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ScoredReport } from '../../src';
55
import {
66
EnrichedAuditReport,
77
EnrichedScoredGroup,
8+
GroupRefInvalidError,
89
ScoredCategoryConfig,
910
} from '../../src/lib/reports/scoring';
1011

@@ -41,7 +42,6 @@ export function deepClone<T>(obj: T): T {
4142
return cloned;
4243
}
4344

44-
// eslint-disable-next-line max-lines-per-function
4545
export function scoreReportOptimized3(report: Report): ScoredReport {
4646
const scoredReport = deepClone(report) as ScoredReport;
4747
const allScoredAuditsAndGroups = new Map<
@@ -64,9 +64,7 @@ export function scoreReportOptimized3(report: Report): ScoredReport {
6464
`${slug}-${ref.slug}-audit`,
6565
)?.score;
6666
if (score == null) {
67-
throw new Error(
68-
`Group has invalid ref - audit with slug ${slug}-${ref.slug}-audit not found`,
69-
);
67+
throw new GroupRefInvalidError(ref.slug, slug);
7068
}
7169
return score;
7270
}

0 commit comments

Comments
 (0)