-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathnx-monorepo.e2e.test.ts
More file actions
219 lines (206 loc) · 6.31 KB
/
nx-monorepo.e2e.test.ts
File metadata and controls
219 lines (206 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { readFile, rename, writeFile } from 'node:fs/promises';
import path from 'node:path';
import type { SimpleGit } from 'simple-git';
import {
type GitRefs,
type Options,
type ProjectRunResult,
type RunResult,
runInCI,
} from '@code-pushup/ci';
import { TEST_SNAPSHOTS_DIR } from '@code-pushup/test-utils';
import { readJsonFile } from '@code-pushup/utils';
import { MOCK_API, MOCK_COMMENT } from '../mocks/api.js';
import { type TestRepo, setupTestRepo } from '../mocks/setup.js';
describe('CI - monorepo mode (Nx)', () => {
let repo: TestRepo;
let git: SimpleGit;
let options: Options;
beforeEach(async () => {
repo = await setupTestRepo('nx-monorepo');
git = repo.git;
options = {
monorepo: true,
directory: repo.baseDir,
parallel: true,
silent: true, // comment out for debugging
};
});
afterEach(async () => {
await repo.cleanup();
});
describe('push event', () => {
beforeEach(async () => {
await git.checkout('main');
});
it('should collect reports for all projects', async () => {
await expect(
runInCI(
{ head: { ref: 'main', sha: await git.revparse('main') } },
MOCK_API,
options,
git,
),
).resolves.toEqual({
mode: 'monorepo',
projects: expect.arrayContaining<ProjectRunResult>([
{
name: 'api',
files: {
current: {
json: path.join(
repo.baseDir,
'.code-pushup/.ci/api/.current/report.json',
),
md: path.join(
repo.baseDir,
'.code-pushup/.ci/api/.current/report.md',
),
},
},
},
]),
} satisfies RunResult);
await expect(
readJsonFile(
path.join(repo.baseDir, '.code-pushup/.ci/api/.current/report.json'),
),
).resolves.toEqual(
expect.objectContaining({
plugins: [
expect.objectContaining({
audits: [
expect.objectContaining({
score: 0,
displayValue: '0% converted',
}),
],
}),
],
}),
);
await expect(
readJsonFile(
path.join(repo.baseDir, '.code-pushup/.ci/ui/.current/report.json'),
),
).resolves.toEqual(
expect.objectContaining({
plugins: [
expect.objectContaining({
audits: [
expect.objectContaining({
score: expect.closeTo(0.666),
displayValue: '67% converted',
}),
],
}),
],
}),
);
});
});
describe('pull request event', () => {
let refs: GitRefs;
beforeEach(async () => {
await git.checkoutLocalBranch('feature-1');
await rename(
path.join(repo.baseDir, 'apps/api/src/index.js'),
path.join(repo.baseDir, 'apps/api/src/index.ts'),
);
await rename(
path.join(repo.baseDir, 'apps/web/src/index.ts'),
path.join(repo.baseDir, 'apps/web/src/index.js'),
);
await rename(
path.join(repo.baseDir, 'libs/ui/code-pushup.config.js'),
path.join(repo.baseDir, 'libs/ui/code-pushup.config.ts'),
);
await writeFile(
path.join(repo.baseDir, 'libs/ui/project.json'),
(
await readFile(
path.join(repo.baseDir, 'libs/ui/project.json'),
'utf8',
)
).replace('code-pushup.config.js', 'code-pushup.config.ts'),
);
await git.add('.');
await git.commit('Convert JS->TS for api and ui, TS->JS for web');
refs = {
head: { ref: 'feature-1', sha: await git.revparse('feature-1') },
base: { ref: 'main', sha: await git.revparse('main') },
};
});
it('should compare reports for all projects, detect new issues and merge into Markdown comment', async () => {
await expect(runInCI(refs, MOCK_API, options, git)).resolves.toEqual({
mode: 'monorepo',
commentId: MOCK_COMMENT.id,
files: {
comparison: {
md: path.join(
repo.baseDir,
'.code-pushup/.ci/.comparison/report-diff.md',
),
},
},
projects: expect.arrayContaining<ProjectRunResult>([
{
name: 'web',
files: {
current: {
json: path.join(
repo.baseDir,
'.code-pushup/.ci/web/.current/report.json',
),
md: path.join(
repo.baseDir,
'.code-pushup/.ci/web/.current/report.md',
),
},
previous: {
json: path.join(
repo.baseDir,
'.code-pushup/.ci/web/.previous/report.json',
),
md: path.join(
repo.baseDir,
'.code-pushup/.ci/web/.previous/report.md',
),
},
comparison: {
json: path.join(
repo.baseDir,
'.code-pushup/.ci/web/.comparison/report-diff.json',
),
md: path.join(
repo.baseDir,
'.code-pushup/.ci/web/.comparison/report-diff.md',
),
},
},
newIssues: [
{
message: 'Use .ts file extension instead of .js',
severity: 'warning',
source: { file: 'apps/web/src/index.js' },
plugin: expect.objectContaining({ slug: 'ts-migration' }),
audit: expect.objectContaining({ slug: 'ts-files' }),
},
],
},
]),
} satisfies RunResult);
const mdPromise = readFile(
path.join(repo.baseDir, '.code-pushup/.ci/.comparison/report-diff.md'),
'utf8',
);
await expect(mdPromise).resolves.toBeTruthy();
const md = await mdPromise;
await expect(
md.replace(/[\da-f]{40}/g, '`<commit-sha>`'),
).toMatchFileSnapshot(
path.join(TEST_SNAPSHOTS_DIR, 'nx-monorepo-report-diff.md'),
);
}, 120_000);
});
});