Skip to content

Commit 51b35db

Browse files
committed
test(plugin-js-packages-e2e): create vulnerabilities e2e test
1 parent 2a05a16 commit 51b35db

File tree

8 files changed

+232
-0
lines changed

8 files changed

+232
-0
lines changed
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import jsPackagesPlugin from '@code-pushup/js-packages-plugin';
2+
import type { CoreConfig } from '@code-pushup/models';
3+
4+
export default {
5+
persist: {
6+
outputDir: './',
7+
filename: 'report',
8+
format: ['json'],
9+
},
10+
plugins: [await jsPackagesPlugin({ packageManager: 'npm' })],
11+
categories: [
12+
{
13+
slug: 'security',
14+
title: 'Security',
15+
refs: [
16+
{ type: 'group', plugin: 'js-packages', slug: 'npm-audit', weight: 1 },
17+
],
18+
},
19+
{
20+
slug: 'updates',
21+
title: 'Updates',
22+
refs: [
23+
{
24+
type: 'group',
25+
plugin: 'js-packages',
26+
slug: 'npm-outdated',
27+
weight: 1,
28+
},
29+
],
30+
},
31+
],
32+
} satisfies CoreConfig;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"dependencies": {
3+
"express": "3.0.0"
4+
},
5+
"devDependencies": {
6+
"@code-pushup/cli": "latest",
7+
"@code-pushup/js-packages-plugin": "latest",
8+
"@code-pushup/models": "latest"
9+
}
10+
}
+23
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+
}
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+
});
+20
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+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference types="vitest" />
2+
import { defineConfig } from 'vite';
3+
import { tsconfigPathAliases } from '../../tools/vitest-tsconfig-path-aliases.js';
4+
5+
export default defineConfig({
6+
cacheDir: '../../node_modules/.vite/plugin-js-packages-e2e',
7+
test: {
8+
reporters: ['basic'],
9+
testTimeout: 120_000,
10+
globals: true,
11+
alias: tsconfigPathAliases(),
12+
pool: 'threads',
13+
poolOptions: { threads: { singleThread: true } },
14+
cache: {
15+
dir: '../../node_modules/.vitest',
16+
},
17+
environment: 'node',
18+
include: ['tests/**/*.e2e.test.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
19+
setupFiles: ['../../testing/test-setup/src/lib/reset.mocks.ts'],
20+
},
21+
});

0 commit comments

Comments
 (0)