-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
run.test.ts
448 lines (395 loc) · 14 KB
/
run.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import {
checkFilesExist,
cleanupProject,
isWindows,
newProject,
readFile,
readJson,
readProjectConfig,
removeFile,
runCLI,
runCLIAsync,
runCommand,
uniq,
updateFile,
updateProjectConfig,
} from '@nrwl/e2e/utils';
import { PackageJson } from 'nx/src/utils/package-json';
describe('Nx Running Tests', () => {
let proj: string;
beforeAll(() => (proj = newProject()));
afterAll(() => cleanupProject());
describe('running targets', () => {
describe('(forwarding params)', () => {
let proj = uniq('proj');
beforeAll(() => {
runCLI(`generate @nrwl/workspace:lib ${proj}`);
updateProjectConfig(proj, (c) => {
c.targets['echo'] = {
executor: 'nx:run-commands',
options: {
command: 'echo ECHO:',
},
};
return c;
});
});
it.each([
'--watch false',
'--watch=false',
'--arr=a,b,c',
'--arr=a --arr=b --arr=c',
'a',
'--a.b=1',
'--a.b 1',
'-- a b c --a --a.b=1',
'--ignored -- a b c --a --a.b=1',
])('should forward %s properly', (args) => {
const output = runCLI(`echo ${proj} ${args}`);
expect(output).toContain(`ECHO: ${args.replace(/^.*-- /, '')}`);
});
});
it('should execute long running tasks', async () => {
const myapp = uniq('myapp');
runCLI(`generate @nrwl/web:app ${myapp}`);
updateProjectConfig(myapp, (c) => {
c.targets['counter'] = {
executor: '@nrwl/workspace:counter',
options: {
to: 2,
},
};
return c;
});
const success = runCLI(`counter ${myapp} --result=true`);
expect(success).toContain('0');
expect(success).toContain('1');
expect(() => runCLI(`counter ${myapp} --result=false`)).toThrowError();
});
it('should run npm scripts', async () => {
const mylib = uniq('mylib');
runCLI(`generate @nrwl/node:lib ${mylib}`);
// Used to restore targets to lib after test
const original = readProjectConfig(mylib);
updateProjectConfig(mylib, (j) => {
delete j.targets;
return j;
});
updateFile(
`libs/${mylib}/package.json`,
JSON.stringify(<PackageJson>{
name: 'mylib1',
version: '1.0.0',
scripts: { 'echo:dev': `echo ECHOED`, 'echo:fail': 'should not run' },
nx: {
includedScripts: ['echo:dev'],
},
})
);
const { stdout } = await runCLIAsync(
`echo:dev ${mylib} -- positional --a=123 --no-b`,
{
silent: true,
}
);
if (isWindows()) {
expect(stdout).toMatch(/ECHOED "positional" "--a=123" "--no-b"/);
} else {
expect(stdout).toMatch(/ECHOED positional --a=123 --no-b/);
}
expect(runCLI(`echo:fail ${mylib}`, { silenceError: true })).toContain(
`Cannot find configuration for task ${mylib}:echo:fail`
);
updateProjectConfig(mylib, (c) => original);
}, 1000000);
});
describe('Nx Bail', () => {
it('should stop executing all tasks when one of the tasks fails', async () => {
const myapp1 = uniq('a');
const myapp2 = uniq('b');
runCLI(`generate @nrwl/web:app ${myapp1}`);
runCLI(`generate @nrwl/web:app ${myapp2}`);
updateProjectConfig(myapp1, (c) => {
c.targets['error'] = {
executor: 'nx:run-commands',
options: {
command: 'echo boom1 && exit 1',
},
};
return c;
});
updateProjectConfig(myapp2, (c) => {
c.targets['error'] = {
executor: 'nx:run-commands',
options: {
command: 'echo boom2 && exit 1',
},
};
return c;
});
let withoutBail = runCLI(`run-many --target=error --all --parallel=1`, {
silenceError: true,
})
.split('\n')
.map((r) => r.trim())
.filter((r) => r);
withoutBail = withoutBail.slice(withoutBail.indexOf('Failed tasks:'));
expect(withoutBail).toContain(`- ${myapp1}:error`);
expect(withoutBail).toContain(`- ${myapp2}:error`);
let withBail = runCLI(
`run-many --target=error --all --parallel=1 --nx-bail`,
{
silenceError: true,
}
)
.split('\n')
.map((r) => r.trim())
.filter((r) => r);
withBail = withBail.slice(withBail.indexOf('Failed tasks:'));
expect(withBail).toContain(`- ${myapp1}:error`);
expect(withBail).not.toContain(`- ${myapp2}:error`);
});
});
describe('run-one', () => {
it('should build a specific project', () => {
const myapp = uniq('app');
runCLI(`generate @nrwl/web:app ${myapp}`);
runCLI(`build ${myapp}`);
}, 10000);
it('should run targets from package json', () => {
const myapp = uniq('app');
const target = uniq('script');
const expectedOutput = uniq('myEchoedString');
runCLI(`generate @nrwl/web:app ${myapp}`);
updateFile(
`apps/${myapp}/package.json`,
JSON.stringify({
name: myapp,
scripts: {
[target]: `echo ${expectedOutput}`,
},
})
);
expect(runCLI(`${target} ${myapp}`)).toContain(expectedOutput);
}, 10000);
it('should run targets inferred from plugin-specified project files', () => {
// Setup an app to extend
const myapp = uniq('app');
runCLI(`generate @nrwl/web:app ${myapp}`);
// Register an Nx plugin
const plugin = `module.exports = {
projectFilePatterns: ['inferred-project.nxproject'],
registerProjectTargets: () => ({
"echo": {
"executor": "nx:run-commands",
"options": {
"command": "echo inferred-target"
}
}
})
}`;
updateFile('tools/local-plugin/plugin.js', plugin);
updateFile('nx.json', (c) => {
const nxJson = JSON.parse(c);
nxJson.plugins = ['./tools/local-plugin/plugin.js'];
return JSON.stringify(nxJson, null, 2);
});
// Create a custom project file for the app
updateFile(`apps/${myapp}/inferred-project.nxproject`, 'contents');
expect(runCLI(`echo ${myapp}`)).toContain('inferred-target');
});
it('should build a specific project with the daemon disabled', () => {
const myapp = uniq('app');
runCLI(`generate @nrwl/web:app ${myapp}`);
const buildWithDaemon = runCLI(`build ${myapp}`, {
env: { ...process.env, NX_DAEMON: 'false' },
});
expect(buildWithDaemon).toContain('Successfully ran target build');
const buildAgain = runCLI(`build ${myapp}`, {
env: { ...process.env, NX_DAEMON: 'false' },
});
expect(buildAgain).toContain('[local cache]');
}, 10000);
it('should build the project when within the project root', () => {
const myapp = uniq('app');
runCLI(`generate @nrwl/web:app ${myapp}`);
// Should work within the project directory
expect(runCommand(`cd apps/${myapp}/src && npx nx build`)).toContain(
`nx run ${myapp}:build:production`
);
}, 10000);
describe('target dependencies', () => {
let myapp;
let mylib1;
let mylib2;
beforeAll(() => {
myapp = uniq('myapp');
mylib1 = uniq('mylib1');
mylib2 = uniq('mylib1');
runCLI(`generate @nrwl/web:app ${myapp}`);
runCLI(`generate @nrwl/web:lib ${mylib1} --buildable`);
runCLI(`generate @nrwl/web:lib ${mylib2} --buildable`);
updateFile(
`apps/${myapp}/src/main.ts`,
`
import "@${proj}/${mylib1}";
import "@${proj}/${mylib2}";
`
);
});
it('should be able to include deps using target dependencies', () => {
const originalWorkspace = readProjectConfig(myapp);
updateProjectConfig(myapp, (config) => {
config.targets.prep = {
executor: 'nx:run-commands',
options: {
command: 'echo PREP',
},
};
config.targets.build.dependsOn = ['prep', '^build'];
return config;
});
const output = runCLI(`build ${myapp}`);
expect(output).toContain(
`NX Running target build for project ${myapp} and 3 task(s) it depends on`
);
expect(output).toContain(myapp);
expect(output).toContain(mylib1);
expect(output).toContain(mylib2);
expect(output).toContain('PREP');
updateProjectConfig(myapp, () => originalWorkspace);
}, 10000);
it('should be able to include deps using target dependencies defined at the root', () => {
const originalNxJson = readFile('nx.json');
const nxJson = readJson('nx.json');
nxJson.targetDependencies = {
build: ['^build', '^e2e-extra-entry-to-bust-cache'],
};
updateFile('nx.json', JSON.stringify(nxJson));
const output = runCLI(`build ${myapp}`);
expect(output).toContain(
`NX Running target build for project ${myapp} and 2 task(s) it depends on`
);
expect(output).toContain(myapp);
expect(output).toContain(mylib1);
expect(output).toContain(mylib2);
updateFile('nx.json', originalNxJson);
}, 10000);
it('should be able to include deps using target defaults defined at the root', () => {
const nxJson = readJson('nx.json');
updateProjectConfig(myapp, (config) => {
config.targets.prep = {
executor: 'nx:run-commands',
options: {
command: 'echo PREP > one.txt',
},
};
config.targets.outside = {
executor: 'nx:run-commands',
options: {
command: 'echo OUTSIDE',
},
};
return config;
});
nxJson.tasksRunnerOptions.default.options.cacheableOperations = [
'prep',
'outside',
];
nxJson.targetDefaults = {
prep: {
outputs: ['{workspaceRoot}/one.txt'],
},
outside: {
dependsOn: ['prep'],
},
};
updateFile('nx.json', JSON.stringify(nxJson));
const output = runCLI(`outside ${myapp}`);
expect(output).toContain(
`NX Running target outside for project ${myapp} and 1 task(s) it depends on`
);
removeFile(`one.txt`);
runCLI(`outside ${myapp}`);
checkFilesExist(`one.txt`);
}, 10000);
});
});
describe('run-many', () => {
it('should build specific and all projects', () => {
// This is required to ensure the numbers used in the assertions make sense for this test
const proj = newProject();
const appA = uniq('appa-rand');
const libA = uniq('liba-rand');
const libB = uniq('libb-rand');
const libC = uniq('libc-rand');
const libD = uniq('libd-rand');
runCLI(`generate @nrwl/web:app ${appA}`);
runCLI(`generate @nrwl/js:lib ${libA} --buildable --defaults`);
runCLI(`generate @nrwl/js:lib ${libB} --buildable --defaults`);
runCLI(`generate @nrwl/js:lib ${libC} --buildable --defaults`);
runCLI(`generate @nrwl/node:lib ${libD} --defaults`);
// libA depends on libC
updateFile(
`libs/${libA}/src/lib/${libA}.spec.ts`,
`
import '@${proj}/${libC}';
describe('sample test', () => {
it('should test', () => {
expect(1).toEqual(1);
});
});
`
);
// testing run many starting'
const buildParallel = runCLI(
`run-many --target=build --projects="${libC},${libB}"`
);
expect(buildParallel).toContain(`Running target build for 2 project(s):`);
expect(buildParallel).not.toContain(`- ${libA}`);
expect(buildParallel).toContain(`- ${libB}`);
expect(buildParallel).toContain(`- ${libC}`);
expect(buildParallel).not.toContain(`- ${libD}`);
expect(buildParallel).toContain('Successfully ran target build');
// testing run many --all starting
const buildAllParallel = runCLI(`run-many --target=build`);
expect(buildAllParallel).toContain(
`Running target build for 4 project(s):`
);
expect(buildAllParallel).toContain(`- ${appA}`);
expect(buildAllParallel).toContain(`- ${libA}`);
expect(buildAllParallel).toContain(`- ${libB}`);
expect(buildAllParallel).toContain(`- ${libC}`);
expect(buildAllParallel).not.toContain(`- ${libD}`);
expect(buildAllParallel).toContain('Successfully ran target build');
// testing run many when project depends on other projects
const buildWithDeps = runCLI(
`run-many --target=build --projects="${libA}"`
);
expect(buildWithDeps).toContain(
`Running target build for 1 project(s) and 1 task(s) they depend on:`
);
expect(buildWithDeps).toContain(`- ${libA}`);
expect(buildWithDeps).toContain(`${libC}`); // build should include libC as dependency
expect(buildWithDeps).not.toContain(`- ${libB}`);
expect(buildWithDeps).not.toContain(`- ${libD}`);
expect(buildWithDeps).toContain('Successfully ran target build');
// testing run many --configuration
const buildConfig = runCLI(
`run-many --target=build --projects="${appA},${libA}" --prod`
);
expect(buildConfig).toContain(
`Running target build for 2 project(s) and 1 task(s) they depend on:`
);
expect(buildConfig).toContain(`run ${appA}:build:production`);
expect(buildConfig).toContain(`run ${libA}:build`);
expect(buildConfig).toContain(`run ${libC}:build`);
expect(buildConfig).toContain('Successfully ran target build');
// testing run many with daemon disabled
const buildWithDaemon = runCLI(`run-many --target=build`, {
env: { ...process.env, NX_DAEMON: 'false' },
});
expect(buildWithDaemon).toContain(`Successfully ran target build`);
}, 1000000);
});
});