/
affected.test.ts
263 lines (234 loc) · 8.34 KB
/
affected.test.ts
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import type { NxJsonConfiguration } from '@nrwl/devkit';
import {
getPackageManagerCommand,
isNotWindows,
listFiles,
newProject,
readFile,
readJson,
readProjectConfig,
cleanupProject,
rmDist,
runCLI,
runCLIAsync,
runCommand,
uniq,
updateFile,
updateProjectConfig,
workspaceConfigName,
} from '@nrwl/e2e/utils';
describe('affected:*', () => {
let proj: string;
beforeEach(() => (proj = newProject()));
afterEach(() => cleanupProject());
it('should print, build, and test affected apps', async () => {
process.env.CI = 'true';
const myapp = uniq('myapp');
const myapp2 = uniq('myapp2');
const mylib = uniq('mylib');
const mylib2 = uniq('mylib2');
const mypublishablelib = uniq('mypublishablelib');
runCLI(`generate @nrwl/react:app ${myapp}`);
runCLI(`generate @nrwl/react:app ${myapp2}`);
runCLI(`generate @nrwl/react:lib ${mylib}`);
runCLI(`generate @nrwl/react:lib ${mylib2}`);
runCLI(
`generate @nrwl/react:lib ${mypublishablelib} --publishable --importPath=@${proj}/${mypublishablelib}`
);
updateFile(
`apps/${myapp}/src/app/app.component.spec.ts`,
`
import '@${proj}/${mylib}';
describe('sample test', () => {
it('should test', () => {
expect(1).toEqual(1);
});
});
`
);
updateFile(
`libs/${mypublishablelib}/src/lib/${mypublishablelib}.module.spec.ts`,
`
import '@${proj}/${mylib}';
describe('sample test', () => {
it('should test', () => {
expect(1).toEqual(1);
});
});
`
);
expect(
(
await runCLIAsync(
`affected:apps --files="libs/${mylib}/src/index.ts" --plain`,
{ silent: true }
)
).stdout.trim()
).toEqual(myapp);
const affectedApps = runCLI(
`affected:apps --files="libs/${mylib}/src/index.ts"`
);
expect(affectedApps).toContain(myapp);
expect(affectedApps).not.toContain(myapp2);
expect(affectedApps).not.toContain(`${myapp}-e2e`);
const implicitlyAffectedApps = runCLI(
'affected:apps --files="tsconfig.base.json"'
);
expect(implicitlyAffectedApps).toContain(myapp);
expect(implicitlyAffectedApps).toContain(myapp2);
const noAffectedApps = runCLI('affected:apps --files="README.md"');
expect(noAffectedApps).not.toContain(myapp);
expect(noAffectedApps).not.toContain(myapp2);
expect(
(
await runCLIAsync(
`affected:libs --files="libs/${mylib}/src/index.ts" --plain`,
{ silent: true }
)
).stdout.trim()
).toEqual(`${mylib} ${mypublishablelib}`);
const affectedLibs = runCLI(
`affected:libs --files="libs/${mylib}/src/index.ts"`
);
expect(affectedLibs).toContain(mypublishablelib);
expect(affectedLibs).toContain(mylib);
expect(affectedLibs).not.toContain(mylib2);
const implicitlyAffectedLibs = runCLI(
'affected:libs --files="tsconfig.base.json"'
);
expect(implicitlyAffectedLibs).toContain(mypublishablelib);
expect(implicitlyAffectedLibs).toContain(mylib);
expect(implicitlyAffectedLibs).toContain(mylib2);
const noAffectedLibsNonExistentFile = runCLI(
'affected:libs --files="tsconfig.json"'
);
expect(noAffectedLibsNonExistentFile).not.toContain(mypublishablelib);
expect(noAffectedLibsNonExistentFile).not.toContain(mylib);
expect(noAffectedLibsNonExistentFile).not.toContain(mylib2);
const noAffectedLibs = runCLI('affected:libs --files="README.md"');
expect(noAffectedLibs).not.toContain(mypublishablelib);
expect(noAffectedLibs).not.toContain(mylib);
expect(noAffectedLibs).not.toContain(mylib2);
// build
const build = runCLI(
`affected:build --files="libs/${mylib}/src/index.ts" --parallel`
);
expect(build).toContain(`Running target build for 2 project(s):`);
expect(build).toContain(`- ${myapp}`);
expect(build).toContain(`- ${mypublishablelib}`);
expect(build).not.toContain('is not registered with the build command');
expect(build).toContain('Successfully ran target build');
const buildExcluded = runCLI(
`affected:build --files="libs/${mylib}/src/index.ts" --exclude ${myapp}`
);
expect(buildExcluded).toContain(`Running target build for 1 project(s):`);
expect(buildExcluded).toContain(`- ${mypublishablelib}`);
// test
updateFile(
`apps/${myapp}/src/app/app.component.spec.ts`,
readFile(`apps/${myapp}/src/app/app.component.spec.ts`).replace(
'.toEqual(1)',
'.toEqual(2)'
)
);
const failedTests = runCLI(
`affected:test --files="libs/${mylib}/src/index.ts"`,
{ silenceError: true }
);
expect(failedTests).toContain(mylib);
expect(failedTests).toContain(myapp);
expect(failedTests).toContain(mypublishablelib);
expect(failedTests).toContain(`Failed tasks:`);
// Fix failing Unit Test
updateFile(
`apps/${myapp}/src/app/app.component.spec.ts`,
readFile(`apps/${myapp}/src/app/app.component.spec.ts`).replace(
'.toEqual(2)',
'.toEqual(1)'
)
);
}, 1000000);
});
describe('affected (with git)', () => {
let myapp;
let myapp2;
let mylib;
beforeEach(() => {
myapp = uniq('myapp');
myapp2 = uniq('myapp');
mylib = uniq('mylib');
newProject();
const nxJson: NxJsonConfiguration = readJson('nx.json');
delete nxJson.implicitDependencies;
updateFile('nx.json', JSON.stringify(nxJson));
runCommand(`git init`);
runCommand(`git config user.email "test@test.com"`);
runCommand(`git config user.name "Test"`);
runCommand(`git config commit.gpgsign false`);
try {
runCommand(
`git add . && git commit -am "initial commit" && git checkout -b main`
);
} catch (e) {}
});
afterAll(() => cleanupProject());
function generateAll() {
runCLI(`generate @nrwl/angular:app ${myapp}`);
runCLI(`generate @nrwl/angular:app ${myapp2}`);
runCLI(`generate @nrwl/angular:lib ${mylib}`);
runCommand(`git add . && git commit -am "add all"`);
}
it('should not affect other projects by generating a new project', () => {
// TODO: investigate why affected gives different results on windows
if (isNotWindows()) {
runCLI(`generate @nrwl/angular:app ${myapp}`);
expect(runCLI('affected:apps')).toContain(myapp);
runCommand(`git add . && git commit -am "add ${myapp}"`);
runCLI(`generate @nrwl/angular:app ${myapp2}`);
expect(runCLI('affected:apps')).not.toContain(myapp);
expect(runCLI('affected:apps')).toContain(myapp2);
runCommand(`git add . && git commit -am "add ${myapp2}"`);
runCLI(`generate @nrwl/angular:lib ${mylib}`);
expect(runCLI('affected:apps')).not.toContain(myapp);
expect(runCLI('affected:apps')).not.toContain(myapp2);
expect(runCLI('affected:libs')).toContain(mylib);
}
}, 1000000);
it('should detect changes to projects based on tags changes', () => {
// TODO: investigate why affected gives different results on windows
if (isNotWindows()) {
generateAll();
updateProjectConfig(myapp, (config) => ({
...config,
tags: ['tag'],
}));
expect(runCLI('affected:apps')).toContain(myapp);
expect(runCLI('affected:apps')).not.toContain(myapp2);
expect(runCLI('affected:libs')).not.toContain(mylib);
}
});
it('should detect changes to projects based on the workspace.json', () => {
// TODO: investigate why affected gives different results on windows
if (isNotWindows()) {
generateAll();
updateProjectConfig(myapp, (config) => ({
...config,
prefix: 'my-app',
}));
expect(runCLI('affected:apps')).toContain(myapp);
expect(runCLI('affected:apps')).not.toContain(myapp2);
expect(runCLI('affected:libs')).not.toContain(mylib);
}
});
it('should affect all projects by removing projects', () => {
generateAll();
updateFile(workspaceConfigName(), (old) => {
const workspaceJson = JSON.parse(old);
delete workspaceJson.projects[mylib];
return JSON.stringify(workspaceJson, null, 2);
});
expect(runCLI('affected:apps')).toContain(myapp);
expect(runCLI('affected:apps')).toContain(myapp2);
expect(runCLI('affected:libs')).not.toContain(mylib);
});
});