|
| 1 | +import { readFile, rename, writeFile } from 'node:fs/promises'; |
1 | 2 | import { join } from 'node:path';
|
2 | 3 | import type { SimpleGit } from 'simple-git';
|
3 | 4 | import { afterEach } from 'vitest';
|
4 |
| -import { type Options, type RunResult, runInCI } from '@code-pushup/ci'; |
| 5 | +import { |
| 6 | + type GitRefs, |
| 7 | + type Options, |
| 8 | + type ProjectRunResult, |
| 9 | + type RunResult, |
| 10 | + runInCI, |
| 11 | +} from '@code-pushup/ci'; |
| 12 | +import { TEST_SNAPSHOTS_DIR } from '@code-pushup/test-utils'; |
5 | 13 | import { readJsonFile } from '@code-pushup/utils';
|
6 |
| -import { MOCK_API } from '../mocks/api'; |
| 14 | +import { MOCK_API, MOCK_COMMENT } from '../mocks/api'; |
7 | 15 | import { type TestRepo, setupTestRepo } from '../mocks/setup';
|
8 | 16 |
|
9 | 17 | describe('CI - monorepo mode (Nx)', () => {
|
@@ -39,7 +47,7 @@ describe('CI - monorepo mode (Nx)', () => {
|
39 | 47 | ),
|
40 | 48 | ).resolves.toEqual({
|
41 | 49 | mode: 'monorepo',
|
42 |
| - projects: expect.arrayContaining([ |
| 50 | + projects: expect.arrayContaining<ProjectRunResult>([ |
43 | 51 | {
|
44 | 52 | name: 'api',
|
45 | 53 | files: {
|
@@ -87,5 +95,85 @@ describe('CI - monorepo mode (Nx)', () => {
|
87 | 95 | });
|
88 | 96 | });
|
89 | 97 |
|
90 |
| - // TODO: pull request event |
| 98 | + describe('pull request event', () => { |
| 99 | + let refs: GitRefs; |
| 100 | + |
| 101 | + beforeEach(async () => { |
| 102 | + await git.checkoutLocalBranch('feature-1'); |
| 103 | + |
| 104 | + await rename( |
| 105 | + join(repo.baseDir, 'apps/api/src/index.js'), |
| 106 | + join(repo.baseDir, 'apps/api/src/index.ts'), |
| 107 | + ); |
| 108 | + await rename( |
| 109 | + join(repo.baseDir, 'apps/web/src/index.ts'), |
| 110 | + join(repo.baseDir, 'apps/web/src/index.js'), |
| 111 | + ); |
| 112 | + await rename( |
| 113 | + join(repo.baseDir, 'libs/ui/code-pushup.config.js'), |
| 114 | + join(repo.baseDir, 'libs/ui/code-pushup.config.ts'), |
| 115 | + ); |
| 116 | + await writeFile( |
| 117 | + join(repo.baseDir, 'libs/ui/project.json'), |
| 118 | + ( |
| 119 | + await readFile(join(repo.baseDir, 'libs/ui/project.json'), 'utf8') |
| 120 | + ).replace('code-pushup.config.js', 'code-pushup.config.ts'), |
| 121 | + ); |
| 122 | + |
| 123 | + await git.add('.'); |
| 124 | + await git.commit('Convert JS->TS for api and ui, TS->JS for web'); |
| 125 | + |
| 126 | + refs = { |
| 127 | + head: { ref: 'feature-1', sha: await git.revparse('feature-1') }, |
| 128 | + base: { ref: 'main', sha: await git.revparse('main') }, |
| 129 | + }; |
| 130 | + }); |
| 131 | + |
| 132 | + it('should compare reports for all projects, detect new issues and merge into Markdown comment', async () => { |
| 133 | + await expect(runInCI(refs, MOCK_API, options, git)).resolves.toEqual({ |
| 134 | + mode: 'monorepo', |
| 135 | + commentId: MOCK_COMMENT.id, |
| 136 | + diffPath: join(repo.baseDir, '.code-pushup/merged-report-diff.md'), |
| 137 | + projects: expect.arrayContaining<ProjectRunResult>([ |
| 138 | + { |
| 139 | + name: 'web', |
| 140 | + files: { |
| 141 | + report: { |
| 142 | + json: join(repo.baseDir, 'apps/web/.code-pushup/report.json'), |
| 143 | + md: join(repo.baseDir, 'apps/web/.code-pushup/report.md'), |
| 144 | + }, |
| 145 | + diff: { |
| 146 | + json: join( |
| 147 | + repo.baseDir, |
| 148 | + 'apps/web/.code-pushup/report-diff.json', |
| 149 | + ), |
| 150 | + md: join(repo.baseDir, 'apps/web/.code-pushup/report-diff.md'), |
| 151 | + }, |
| 152 | + }, |
| 153 | + newIssues: [ |
| 154 | + { |
| 155 | + message: 'Use .ts file extension instead of .js', |
| 156 | + severity: 'warning', |
| 157 | + source: { file: 'apps/web/src/index.js' }, |
| 158 | + plugin: expect.objectContaining({ slug: 'ts-migration' }), |
| 159 | + audit: expect.objectContaining({ slug: 'ts-files' }), |
| 160 | + }, |
| 161 | + ], |
| 162 | + }, |
| 163 | + ]), |
| 164 | + } satisfies RunResult); |
| 165 | + |
| 166 | + const mdPromise = readFile( |
| 167 | + join(repo.baseDir, '.code-pushup/merged-report-diff.md'), |
| 168 | + 'utf8', |
| 169 | + ); |
| 170 | + await expect(mdPromise).resolves.toBeTruthy(); |
| 171 | + const md = await mdPromise; |
| 172 | + await expect( |
| 173 | + md.replace(/[\da-f]{40}/g, '`<commit-sha>`'), |
| 174 | + ).toMatchFileSnapshot( |
| 175 | + join(TEST_SNAPSHOTS_DIR, 'nx-monorepo-report-diff.md'), |
| 176 | + ); |
| 177 | + }); |
| 178 | + }); |
91 | 179 | });
|
0 commit comments