Skip to content

Commit cefe058

Browse files
committed
test(plugin-js-packages-e2e): create vulnerabilities e2e test
1 parent b230a3d commit cefe058

File tree

9 files changed

+393
-0
lines changed

9 files changed

+393
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import tseslint from 'typescript-eslint';
2+
import baseConfig from '../../eslint.config.js';
3+
4+
export default tseslint.config(...baseConfig, {
5+
files: ['**/*.ts'],
6+
languageOptions: {
7+
parserOptions: {
8+
projectService: true,
9+
tsconfigRootDir: import.meta.dirname,
10+
},
11+
},
12+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import jsPackagesPlugin from '@code-pushup/js-packages-plugin';
2+
import type { CoreConfig } from '@code-pushup/models';
3+
4+
export default {
5+
persist: { outputDir: './' },
6+
plugins: [await jsPackagesPlugin({ packageManager: 'npm' })],
7+
} satisfies CoreConfig;

e2e/plugin-js-packages-e2e/mocks/fixtures/npm-repo/package-lock.json

Lines changed: 191 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"express": "3.0.0"
4+
}
5+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "plugin-js-packages-e2e",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"projectType": "application",
5+
"sourceRoot": "e2e/plugin-js-packages-e2e/src",
6+
"implicitDependencies": ["cli", "plugin-js-packages"],
7+
"targets": {
8+
"lint": {
9+
"executor": "@nx/linter:eslint",
10+
"outputs": ["{options.outputFile}"],
11+
"options": {
12+
"lintFilePatterns": ["e2e/plugin-eslint-e2e/**/*.ts"]
13+
}
14+
},
15+
"e2e": {
16+
"executor": "@nx/vite:test",
17+
"options": {
18+
"configFile": "e2e/plugin-eslint-e2e/vite.config.e2e.ts"
19+
}
20+
}
21+
},
22+
"tags": ["scope:plugin", "type:e2e"]
23+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { cp } from 'node:fs/promises';
2+
import path from 'node:path';
3+
import { afterAll, beforeAll, expect, it } from 'vitest';
4+
import { type Report, reportSchema } from '@code-pushup/models';
5+
import { nxTargetProject } from '@code-pushup/test-nx-utils';
6+
import { teardownTestFolder } from '@code-pushup/test-setup';
7+
import {
8+
E2E_ENVIRONMENTS_DIR,
9+
TEST_OUTPUT_DIR,
10+
omitVariableReportData,
11+
} from '@code-pushup/test-utils';
12+
import { executeProcess, readJsonFile } from '@code-pushup/utils';
13+
14+
describe('plugin-js-packages', () => {
15+
const fixturesDir = path.join(
16+
'e2e',
17+
'plugin-js-packages-e2e',
18+
'mocks',
19+
'fixtures',
20+
);
21+
const fixturesNPMDir = path.join(fixturesDir, 'npm-repo');
22+
23+
const envRoot = path.join(
24+
E2E_ENVIRONMENTS_DIR,
25+
nxTargetProject(),
26+
TEST_OUTPUT_DIR,
27+
);
28+
const npmRepoDir = path.join(envRoot, 'npm-repo');
29+
30+
beforeAll(async () => {
31+
await cp(fixturesNPMDir, npmRepoDir, { recursive: true });
32+
});
33+
34+
afterAll(async () => {
35+
await teardownTestFolder(npmRepoDir);
36+
});
37+
38+
it('should run JS packages plugin for NPM and create report.json', async () => {
39+
const { code: installCode } = await executeProcess({
40+
command: 'npm',
41+
args: ['install'],
42+
cwd: npmRepoDir,
43+
});
44+
45+
expect(installCode).toBe(0);
46+
47+
const { code, stderr } = await executeProcess({
48+
command: 'npx',
49+
args: ['@code-pushup/cli', 'collect', '--no-progress'],
50+
cwd: npmRepoDir,
51+
});
52+
53+
expect(code).toBe(0);
54+
expect(stderr).toBe('');
55+
56+
const report = await readJsonFile<Report>(
57+
path.join(npmRepoDir, 'report.json'),
58+
);
59+
60+
expect(() => reportSchema.parse(report)).not.toThrow();
61+
expect.objectContaining({
62+
categories: expect.arrayContaining([
63+
expect.objectContaining({ slug: 'security' }),
64+
expect.objectContaining({ slug: 'updates' }),
65+
]),
66+
plugins: expect.arrayContaining([
67+
expect.objectContaining({
68+
packageName: '@code-pushup/js-packages-plugin',
69+
audits: expect.arrayContaining([
70+
expect.objectContaining({
71+
slug: 'npm-audit-prod',
72+
displayValue: expect.stringMatching(/\d vulnerabilities/),
73+
value: expect.closeTo(7, 10), // there are 7 vulnerabilities (6 high, 1 low) at the time
74+
details: expect.objectContaining({
75+
issues: expect.any(Array),
76+
}),
77+
}),
78+
expect.objectContaining({
79+
slug: 'npm-outdated-prod',
80+
displayValue: '1 major outdated package version',
81+
value: 1,
82+
score: 0,
83+
details: {
84+
issues: expect.arrayContaining([
85+
expect.objectContaining({
86+
message: expect.stringMatching(
87+
/Package \[`express`].*\*\*major\*\* update from \*\*\d+\.\d+\.\d+\*\* to \*\*\d+\.\d+\.\d+\*\*/,
88+
),
89+
severity: 'error',
90+
}),
91+
]),
92+
},
93+
}),
94+
]),
95+
}),
96+
]),
97+
});
98+
});
99+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"module": "ESNext",
5+
"forceConsistentCasingInFileNames": true,
6+
"strict": true,
7+
"noImplicitOverride": true,
8+
"noPropertyAccessFromIndexSignature": true,
9+
"noImplicitReturns": true,
10+
"noFallthroughCasesInSwitch": true,
11+
"types": ["vitest"]
12+
},
13+
"files": [],
14+
"include": [],
15+
"references": [
16+
{
17+
"path": "./tsconfig.test.json"
18+
}
19+
]
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "../../dist/out-tsc",
5+
"types": ["vitest/globals", "vitest/importMeta", "vite/client", "node"],
6+
"target": "ES2020"
7+
},
8+
"exclude": ["__test-env__/**"],
9+
"include": [
10+
"vite.config.e2e.ts",
11+
"tests/**/*.e2e.test.ts",
12+
"tests/**/*.d.ts",
13+
"mocks/**/*.ts"
14+
]
15+
}

0 commit comments

Comments
 (0)