-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(plugin-js-packages-e2e): create vulnerabilities e2e test
- Loading branch information
Showing
8 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import tseslint from 'typescript-eslint'; | ||
import baseConfig from '../../eslint.config.js'; | ||
|
||
export default tseslint.config(...baseConfig, { | ||
files: ['**/*.ts'], | ||
languageOptions: { | ||
parserOptions: { | ||
projectService: true, | ||
tsconfigRootDir: import.meta.dirname, | ||
}, | ||
}, | ||
}); |
32 changes: 32 additions & 0 deletions
32
e2e/plugin-js-packages-e2e/mocks/fixtures/npm-repo/code-pushup.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import jsPackagesPlugin from '@code-pushup/js-packages-plugin'; | ||
import type { CoreConfig } from '@code-pushup/models'; | ||
|
||
export default { | ||
persist: { | ||
outputDir: './', | ||
filename: 'report', | ||
format: ['json'], | ||
}, | ||
plugins: [await jsPackagesPlugin({ packageManager: 'npm' })], | ||
categories: [ | ||
{ | ||
slug: 'security', | ||
title: 'Security', | ||
refs: [ | ||
{ type: 'group', plugin: 'js-packages', slug: 'npm-audit', weight: 1 }, | ||
], | ||
}, | ||
{ | ||
slug: 'updates', | ||
title: 'Updates', | ||
refs: [ | ||
{ | ||
type: 'group', | ||
plugin: 'js-packages', | ||
slug: 'npm-outdated', | ||
weight: 1, | ||
}, | ||
], | ||
}, | ||
], | ||
} satisfies CoreConfig; |
10 changes: 10 additions & 0 deletions
10
e2e/plugin-js-packages-e2e/mocks/fixtures/npm-repo/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"dependencies": { | ||
"express": "3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@code-pushup/cli": "latest", | ||
"@code-pushup/js-packages-plugin": "latest", | ||
"@code-pushup/models": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "plugin-js-packages-e2e", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"projectType": "application", | ||
"sourceRoot": "e2e/plugin-js-packages-e2e/src", | ||
"implicitDependencies": ["cli", "plugin-js-packages"], | ||
"targets": { | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["e2e/plugin-eslint-e2e/**/*.ts"] | ||
} | ||
}, | ||
"e2e": { | ||
"executor": "@nx/vite:test", | ||
"options": { | ||
"configFile": "e2e/plugin-eslint-e2e/vite.config.e2e.ts" | ||
} | ||
} | ||
}, | ||
"tags": ["scope:plugin", "type:e2e"] | ||
} |
99 changes: 99 additions & 0 deletions
99
e2e/plugin-js-packages-e2e/tests/plugin-js-packages.e2e.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { cp } from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
import { afterAll, beforeAll, expect, it } from 'vitest'; | ||
import { type Report, reportSchema } from '@code-pushup/models'; | ||
import { nxTargetProject } from '@code-pushup/test-nx-utils'; | ||
import { teardownTestFolder } from '@code-pushup/test-setup'; | ||
import { | ||
E2E_ENVIRONMENTS_DIR, | ||
TEST_OUTPUT_DIR, | ||
omitVariableReportData, | ||
} from '@code-pushup/test-utils'; | ||
import { executeProcess, readJsonFile } from '@code-pushup/utils'; | ||
|
||
describe('plugin-js-packages', () => { | ||
const fixturesDir = path.join( | ||
'e2e', | ||
'plugin-js-packages-e2e', | ||
'mocks', | ||
'fixtures', | ||
); | ||
const fixturesNPMDir = path.join(fixturesDir, 'npm-repo'); | ||
|
||
const envRoot = path.join( | ||
E2E_ENVIRONMENTS_DIR, | ||
nxTargetProject(), | ||
TEST_OUTPUT_DIR, | ||
); | ||
const npmRepoDir = path.join(envRoot, 'npm-repo'); | ||
|
||
beforeAll(async () => { | ||
await cp(fixturesNPMDir, npmRepoDir, { recursive: true }); | ||
}); | ||
|
||
afterAll(async () => { | ||
await teardownTestFolder(npmRepoDir); | ||
}); | ||
|
||
it('should run JS packages plugin for NPM and create report.json', async () => { | ||
const { code: installCode } = await executeProcess({ | ||
command: 'npm', | ||
args: ['install'], | ||
cwd: npmRepoDir, | ||
}); | ||
|
||
expect(installCode).toBe(0); | ||
|
||
const { code, stderr } = await executeProcess({ | ||
command: 'npx', | ||
args: ['@code-pushup/cli', 'collect', '--no-progress'], | ||
cwd: npmRepoDir, | ||
}); | ||
|
||
expect(code).toBe(0); | ||
expect(stderr).toBe(''); | ||
|
||
const report = await readJsonFile<Report>( | ||
path.join(npmRepoDir, 'report.json'), | ||
); | ||
|
||
expect(() => reportSchema.parse(report)).not.toThrow(); | ||
expect.objectContaining({ | ||
categories: expect.arrayContaining([ | ||
expect.objectContaining({ slug: 'security' }), | ||
expect.objectContaining({ slug: 'updates' }), | ||
]), | ||
plugins: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
packageName: '@code-pushup/js-packages-plugin', | ||
audits: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
slug: 'npm-audit-prod', | ||
displayValue: expect.stringMatching(/\d vulnerabilities/), | ||
value: expect.closeTo(7, 10), // there are 7 vulnerabilities (6 high, 1 low) at the time | ||
details: expect.objectContaining({ | ||
issues: expect.any(Array), | ||
}), | ||
}), | ||
expect.objectContaining({ | ||
slug: 'npm-outdated-prod', | ||
displayValue: '1 major outdated package version', | ||
value: 1, | ||
score: 0, | ||
details: { | ||
issues: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
message: expect.stringMatching( | ||
/Package \[`express`].*\*\*major\*\* update from \*\*\d+\.\d+\.\d+\*\* to \*\*\d+\.\d+\.\d+\*\*/, | ||
), | ||
severity: 'error', | ||
}), | ||
]), | ||
}, | ||
}), | ||
]), | ||
}), | ||
]), | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"module": "ESNext", | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"noImplicitOverride": true, | ||
"noPropertyAccessFromIndexSignature": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"types": ["vitest"] | ||
}, | ||
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ | ||
"path": "./tsconfig.test.json" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../dist/out-tsc", | ||
"types": ["vitest/globals", "vitest/importMeta", "vite/client", "node"], | ||
"target": "ES2020" | ||
}, | ||
"exclude": ["__test-env__/**"], | ||
"include": [ | ||
"vite.config.e2e.ts", | ||
"tests/**/*.e2e.test.ts", | ||
"tests/**/*.d.ts", | ||
"mocks/**/*.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/// <reference types="vitest" /> | ||
import { defineConfig } from 'vite'; | ||
import { tsconfigPathAliases } from '../../tools/vitest-tsconfig-path-aliases.js'; | ||
|
||
export default defineConfig({ | ||
cacheDir: '../../node_modules/.vite/plugin-js-packages-e2e', | ||
test: { | ||
reporters: ['basic'], | ||
testTimeout: 120_000, | ||
globals: true, | ||
alias: tsconfigPathAliases(), | ||
pool: 'threads', | ||
poolOptions: { threads: { singleThread: true } }, | ||
cache: { | ||
dir: '../../node_modules/.vitest', | ||
}, | ||
environment: 'node', | ||
include: ['tests/**/*.e2e.test.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], | ||
setupFiles: ['../../testing/test-setup/src/lib/reset.mocks.ts'], | ||
}, | ||
}); |