|
| 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 | +}); |
0 commit comments