Skip to content

Commit 8f421a6

Browse files
committed
test: add e2e tests
1 parent 2688343 commit 8f421a6

File tree

3 files changed

+239
-24
lines changed

3 files changed

+239
-24
lines changed

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

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,6 @@ describe('CLI collect', () => {
6767
expect(md).toContain(dummyAuditTitle);
6868
});
6969

70-
it('should not create reports if --persist.skipReports is given', async () => {
71-
const { code } = await executeProcess({
72-
command: 'npx',
73-
args: [
74-
'@code-pushup/cli',
75-
'--no-progress',
76-
'collect',
77-
'--persist.skipReports',
78-
],
79-
cwd: dummyDir,
80-
});
81-
82-
expect(code).toBe(0);
83-
84-
await expect(
85-
fileExists(path.join(dummyOutputDir, 'report.md')),
86-
).resolves.toBeFalsy();
87-
await expect(
88-
fileExists(path.join(dummyOutputDir, 'report.json')),
89-
).resolves.toBeFalsy();
90-
});
91-
9270
it('should write runner outputs if --cache is given', async () => {
9371
const { code } = await executeProcess({
9472
command: 'npx',
@@ -105,8 +83,8 @@ describe('CLI collect', () => {
10583
).resolves.toStrictEqual([
10684
{
10785
slug: 'dummy-audit',
108-
score: 1,
109-
value: 10,
86+
score: 0.3,
87+
value: 3,
11088
},
11189
]);
11290
});

packages/cli/docs/nx-caching.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Caching Example Nx
2+
3+
To cache plugin runner output, you can use the `--cache.write` and `--cache.read` options in combination with `--onlyPlugins` and `--persist.skipReports` command options.
4+
5+
## `{projectRoot}/code-pushup.config.ts`
6+
7+
```ts
8+
import coveragePlugin from '@code-pushup/coverage-plugin';
9+
import jsPackagesPlugin from '@code-pushup/js-packages-plugin';
10+
import type { CoreConfig } from '@code-pushup/models';
11+
12+
export default {
13+
plugins: [
14+
await coveragePlugin({
15+
reports: ['coverage/lcov.info'],
16+
coverageTypes: ['function', 'branch', 'line'],
17+
}),
18+
await jsPackagesPlugin({
19+
packageManager: 'npm',
20+
}),
21+
],
22+
upload: {
23+
server: 'https://portal.code-pushup.dev/api',
24+
organization: 'my-org',
25+
project: 'lib-a',
26+
apiKey: process.env.CP_API_KEY,
27+
},
28+
} satisfies CoreConfig;
29+
```
30+
31+
## `nx.json`
32+
33+
```json
34+
{
35+
"defaultTarget": {
36+
"code-pushup": {
37+
"cache": true,
38+
"outputs": ["{options.outputPath}"],
39+
"executor": "nx:run-commands",
40+
"options": {
41+
"command": "node packages/cli/src/index.ts",
42+
"config": "{projectRoot}/code-pushup.config.ts",
43+
"cache.read": true,
44+
"upload.project": "{projectName}",
45+
"outputPath": "{projectRoot}/.code-pushup"
46+
}
47+
}
48+
},
49+
"code-pushup:coverage": {
50+
"cache": true,
51+
"outputs": ["{options.outputPath}"],
52+
"executor": "nx:run-commands",
53+
"options": {
54+
"command": "npx @code-pushup/cli collect",
55+
"config": "{projectRoot}/code-pushup.config.ts",
56+
"cache.write": true,
57+
"persist.skipReports": true,
58+
"persist.outputDir": "{projectRoot}/.code-pushup",
59+
"upload.project": "{projectName}",
60+
"outputPath": "{projectRoot}/.code-pushup/coverage"
61+
}
62+
}
63+
}
64+
```
65+
66+
## `{projectRoot}/package.json`
67+
68+
```json
69+
{
70+
"name": "lib-a",
71+
"targets": {
72+
"int-test": {
73+
"cache": true,
74+
"outputs": ["{options.coverage.reportsDirectory}"],
75+
"executor": "@nx/vite:test",
76+
"options": {
77+
"configFile": "packages/lib-a/vitest.int.config.ts",
78+
"coverage.reportsDirectory": "{projectRoot}/coverage/int-test"
79+
}
80+
},
81+
"unit-test": {
82+
"cache": true,
83+
"outputs": ["{options.coverage.reportsDirectory}"],
84+
"executor": "@nx/vite:test",
85+
"options": {
86+
"configFile": "packages/lib-a/vitest.unit.config.ts",
87+
"coverage.reportsDirectory": "{projectRoot}/coverage/unit-test"
88+
}
89+
},
90+
"code-pushup:coverage": {
91+
"dependsOn": ["unit-test", "int-test"]
92+
},
93+
"code-pushup": {
94+
"dependsOn": ["code-pushup:coverage"]
95+
}
96+
}
97+
}
98+
```
99+
100+
## Nx Task Graph
101+
102+
This configuration creates the following task dependency graph:
103+
104+
**Legend:**
105+
106+
- 🐳 = Cached target
107+
108+
```mermaid
109+
graph TD
110+
A[lib-a:code-pushup 🐳] --> B[lib-a:code-pushup:coverage]
111+
A --> E[lib-a:code-pushup:js-packages]
112+
B --> C[lib-a:unit-test 🐳]
113+
B --> D[lib-a:int-test 🐳]
114+
```
115+
116+
## Command Line Example
117+
118+
```bash
119+
# Run all affected projects e.g. nx run lib-a:code-pushup:coverage
120+
nx affected --target=code-pushup:*
121+
122+
# Run all affected projects and upload the report to the portal
123+
nx run reop-source:code-pushup autorun
124+
```
125+
126+
This approach has the following benefits:
127+
128+
1. **Parallel Execution**: Plugins can run in parallel
129+
2. **Finegrained Caching**: Code level cache invalidation enables usage of [affected](https://nx.dev/recipes/affected-tasks) command
130+
3. **Dependency Management**: Leverage Nx task dependencies and its caching strategy
131+
4. **Clear Separation**: Each plugin has its own target for better debugging and maintainability

packages/cli/docs/turbo-caching.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Caching Example Turborepo
2+
3+
To cache plugin runner output with Turborepo, wire Code Pushup into your turbo.json pipeline and pass Code Pushup flags (`--cache.write`, `--cache.read`, `--onlyPlugins`, `--persist.skipReports`) through task scripts. Turborepo will cache task outputs declared in outputs, and you can target affected packages with `--filter=[origin/main]`.
4+
5+
## `{projectRoot}/code-pushup.config.ts`
6+
7+
```ts
8+
import coveragePlugin from '@code-pushup/coverage-plugin';
9+
import jsPackagesPlugin from '@code-pushup/js-packages-plugin';
10+
import type { CoreConfig } from '@code-pushup/models';
11+
12+
export default {
13+
plugins: [
14+
await coveragePlugin({
15+
reports: ['coverage/lcov.info'],
16+
coverageTypes: ['function', 'branch', 'line'],
17+
}),
18+
await jsPackagesPlugin({
19+
packageManager: 'npm',
20+
}),
21+
],
22+
upload: {
23+
server: 'https://portal.code-pushup.dev/api',
24+
organization: 'my-org',
25+
project: 'lib-a',
26+
apiKey: process.env.CP_API_KEY,
27+
},
28+
} satisfies CoreConfig;
29+
```
30+
31+
## Root `turbo.json`
32+
33+
```json
34+
{
35+
"$schema": "https://turbo.build/schema.json",
36+
"pipeline": {
37+
"unit-test": {
38+
"outputs": ["coverage/unit-test/**"]
39+
},
40+
"int-test": {
41+
"outputs": ["coverage/int-test/**"]
42+
},
43+
"code-pushup:coverage": {
44+
"dependsOn": ["unit-test", "int-test"],
45+
"outputs": [".code-pushup/coverage/**"]
46+
},
47+
"code-pushup:js-packages": {
48+
"outputs": [".code-pushup/js-packages/**"]
49+
},
50+
"code-pushup": {
51+
"dependsOn": ["code-pushup:coverage", "code-pushup:js-packages"],
52+
"outputs": [".code-pushup/**"]
53+
}
54+
}
55+
}
56+
```
57+
58+
## `packages/lib-a/package.json`
59+
60+
```json
61+
{
62+
"name": "lib-a",
63+
"scripts": {
64+
"unit-test": "vitest --config packages/lib-a/vitest.unit.config.ts --coverage",
65+
"int-test": "vitest --config packages/lib-a/vitest.int.config.ts --coverage",
66+
"code-pushup:coverage": "code-pushup collect --config packages/lib-a/code-pushup.config.ts --cache.write --persist.skipReports --persist.outputDir packages/lib-a/.code-pushup --onlyPlugins=coverage",
67+
"code-pushup:js-packages": "code-pushup collect --config packages/lib-a/code-pushup.config.ts --cache.write --persist.skipReports --persist.outputDir packages/lib-a/.code-pushup --onlyPlugins=js-packages",
68+
"code-pushup": "code-pushup autorun --config packages/lib-a/code-pushup.config.ts --cache.read --persist.outputDir packages/lib-a/.code-pushup"
69+
}
70+
}
71+
```
72+
73+
> **Note:** `--cache.write` is used on the collect step to persist each plugin's audit-outputs.json; `--cache.read` is used on the autorun step to reuse those outputs.
74+
75+
## Turborepo Task Graph
76+
77+
This configuration creates the following task dependency graph:
78+
79+
**Legend:**
80+
81+
- ⚡ = Cached target (via outputs)
82+
83+
```mermaid
84+
graph TD
85+
A[lib-a:code-pushup ⚡] --> B[lib-a:code-pushup:coverage]
86+
A --> E[lib-a:code-pushup:js-packages]
87+
B --> C[lib-a:unit-test ⚡]
88+
B --> D[lib-a:int-test ⚡]
89+
```
90+
91+
## Command Line Examples
92+
93+
```bash
94+
# Run all affected packages e.g. turbo run code-pushup:coverage --filter=lib-a
95+
turbo run code-pushup:* --filter=[origin/main]
96+
97+
# Run all affected packages and upload the report to the portal
98+
turbo run code-pushup --filter=[origin/main]
99+
```
100+
101+
This approach has the following benefits:
102+
103+
1. **Parallel Execution**: Plugins can run in parallel
104+
2. **Finegrained Caching**: Code level cache invalidation enables usage of affected packages filtering
105+
3. **Dependency Management**: Leverage Turborepo task dependencies and its caching strategy
106+
4. **Clear Separation**: Each plugin has its own target for better debugging and maintainability

0 commit comments

Comments
 (0)