/
bootstrap-command.test.js
691 lines (531 loc) · 21.2 KB
/
bootstrap-command.test.js
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
"use strict";
jest.mock("@lerna/rimraf-dir");
jest.mock("@lerna/npm-install");
jest.mock("@lerna/run-lifecycle");
jest.mock("@lerna/create-symlink");
const fs = require("fs-extra");
const path = require("path");
// mocked or stubbed modules
const rimrafDir = require("@lerna/rimraf-dir");
const npmInstall = require("@lerna/npm-install");
const runLifecycle = require("@lerna/run-lifecycle");
const createSymlink = require("@lerna/create-symlink");
const hasNpmVersion = require("@lerna/has-npm-version");
// helpers
const initFixture = require("@lerna-test/init-fixture")(__dirname);
const normalizeRelativeDir = require("@lerna-test/normalize-relative-dir");
const updateLernaConfig = require("@lerna-test/update-lerna-config");
// file under test
const lernaBootstrap = require("@lerna-test/command-runner")(require("../command"));
// assertion helpers
const installedPackagesInDirectories = testDir =>
npmInstall.dependencies.mock.calls.reduce((obj, [pkg, dependencies]) => {
const relative = normalizeRelativeDir(testDir, pkg.location);
obj[relative || "ROOT"] = dependencies;
return obj;
}, {});
const removedDirectories = testDir =>
rimrafDir.mock.calls.map(([directory]) => normalizeRelativeDir(testDir, directory));
const symlinkedDirectories = testDir =>
createSymlink.mock.calls
.slice()
// ensure sort is always consistent, despite promise variability
.sort((a, b) => {
// two-dimensional path sort
if (b[0] === a[0]) {
if (b[1] === a[1]) {
// ignore third field
return 0;
}
return b[1] < a[1] ? 1 : -1;
}
return b[0] < a[0] ? 1 : -1;
})
.map(([src, dest, type]) => ({
_src: normalizeRelativeDir(testDir, src),
dest: normalizeRelativeDir(testDir, dest),
type,
}));
describe("BootstrapCommand", () => {
// stub rimraf because we trust isaacs
rimrafDir.mockResolvedValue();
// we stub npmInstall in most tests because
// we already have enough tests of npmInstall
npmInstall.mockResolvedValue();
npmInstall.dependencies.mockResolvedValue();
// stub runLifecycle because it is a huge source
// of slowness when running tests for no good reason
runLifecycle.mockResolvedValue();
// the underlying implementation of symlinkBinary and symlinkDependencies
createSymlink.mockResolvedValue();
describe("lifecycle scripts", () => {
const npmLifecycleEvent = process.env.npm_lifecycle_event;
afterEach(() => {
process.env.npm_lifecycle_event = npmLifecycleEvent;
delete process.env.LERNA_EXEC_PATH;
delete process.env.LERNA_ROOT_PATH;
});
it("should run preinstall, postinstall and prepublish scripts", async () => {
const testDir = await initFixture("lifecycle-scripts");
await lernaBootstrap(testDir)();
expect(runLifecycle.getOrderedCalls()).toEqual([
["package-preinstall", "preinstall"],
["package-postinstall", "postinstall"],
["package-prepublish", "prepublish"],
]);
});
it("does not run prepublish scripts with --ignore-prepublish", async () => {
const testDir = await initFixture("lifecycle-scripts");
await lernaBootstrap(testDir)("--ignore-prepublish");
expect(runLifecycle.getOrderedCalls()).toEqual([
["package-preinstall", "preinstall"],
["package-postinstall", "postinstall"],
]);
});
it("shouldn't run lifecycle scripts with --ignore-scripts", async () => {
const testDir = await initFixture("ignored-scripts");
await lernaBootstrap(testDir)("--ignore-scripts");
expect(runLifecycle).not.toHaveBeenCalled();
expect(npmInstall.dependencies).toHaveBeenCalledWith(
expect.objectContaining({
name: "package-prepare",
}),
["tiny-tarball@^1.0.0"],
expect.objectContaining({
npmClientArgs: ["--ignore-scripts"],
})
);
});
it("should not recurse from hoisted root lifecycle", async () => {
const testDir = await initFixture("lifecycle-scripts");
process.env.LERNA_EXEC_PATH = testDir;
process.env.LERNA_ROOT_PATH = testDir;
await lernaBootstrap(testDir)();
expect(runLifecycle).not.toHaveBeenCalled();
});
});
describe("with hoisting", () => {
it("should hoist", async () => {
const testDir = await initFixture("basic");
await lernaBootstrap(testDir)("--hoist");
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
expect(removedDirectories(testDir)).toMatchSnapshot();
// root includes explicit dependencies and hoisted from leaves
expect(npmInstall.dependencies).toHaveBeenCalledWith(
expect.objectContaining({
name: "basic",
}),
["bar@^2.0.0", "foo@^1.0.0", "@test/package-1@^0.0.0"],
{
registry: undefined,
npmClient: "npm",
npmClientArgs: [],
mutex: undefined,
// npmGlobalStyle is not included at all
}
);
// foo@0.1.2 differs from the more common foo@^1.0.0
expect(npmInstall.dependencies).toHaveBeenLastCalledWith(
expect.objectContaining({
name: "package-3",
}),
["foo@0.1.12"],
expect.objectContaining({
npmGlobalStyle: true,
})
);
});
it("should not hoist when disallowed", async () => {
const testDir = await initFixture("basic");
await lernaBootstrap(testDir)("--hoist", "--nohoist", "@test/package-1");
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
expect(removedDirectories(testDir)).toMatchSnapshot();
});
it("should not hoist when disallowed from lerna.json", async () => {
const testDir = await initFixture("basic");
await updateLernaConfig(testDir, {
hoist: true,
nohoist: ["@test/package-1"],
});
await lernaBootstrap(testDir)();
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
});
});
describe("with --npm-client and --hoist", () => {
it("should throw", async () => {
expect.assertions(1);
const testDir = await initFixture("yarn-hoist");
try {
await lernaBootstrap(testDir)();
} catch (err) {
expect(err.message).toMatch(
"--hoist is not supported with --npm-client=yarn, use yarn workspaces instead"
);
}
});
});
describe("with --ci", () => {
it("should call npmInstall with ci subCommand if on npm 5.7.0 or later", async () => {
const testDir = await initFixture("ci");
await lernaBootstrap(testDir)("--ci");
expect(hasNpmVersion).toHaveBeenLastCalledWith(">=5.7.0");
expect(npmInstall.dependencies.mock.calls[0][2]).toEqual({
subCommand: "ci",
registry: undefined,
npmClient: "npm",
npmClientArgs: [],
npmGlobalStyle: false,
mutex: undefined,
});
});
it("should not pass subCommand to npmInstall if on npm version earlier than 5.7.0", async () => {
const testDir = await initFixture("ci");
hasNpmVersion.mockReturnValueOnce(false);
await lernaBootstrap(testDir)("--ci");
expect(npmInstall.dependencies.mock.calls[0][2]).toEqual({
registry: undefined,
npmClient: "npm",
npmClientArgs: [],
npmGlobalStyle: false,
mutex: undefined,
});
});
it("should not run npm ci when hoisting", async () => {
const testDir = await initFixture("basic");
await lernaBootstrap(testDir)("--hoist", "package-*", "--ci");
expect(npmInstall.dependencies.mock.calls[0][2]).toMatchObject({
subCommand: "install", // not "ci"
npmClient: "npm",
npmClientArgs: ["--no-save"],
});
});
it("respects config file { ci: false }", async () => {
const testDir = await initFixture("ci");
await updateLernaConfig(testDir, {
command: {
bootstrap: {
ci: false,
},
},
});
await lernaBootstrap(testDir)();
expect(hasNpmVersion).not.toHaveBeenCalled();
expect(npmInstall.dependencies.mock.calls[0][2]).toEqual({
registry: undefined,
npmClient: "npm",
npmClientArgs: [],
npmGlobalStyle: false,
mutex: undefined,
});
});
});
describe("with local package dependencies", () => {
it("should bootstrap packages", async () => {
const testDir = await initFixture("basic");
await lernaBootstrap(testDir)();
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
expect(symlinkedDirectories(testDir)).toMatchSnapshot();
});
it("should not bootstrap ignored packages", async () => {
const testDir = await initFixture("basic");
await lernaBootstrap(testDir)("--ignore", "{@test/package-2,package-4}");
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
expect(symlinkedDirectories(testDir)).toMatchSnapshot();
});
it("should only bootstrap scoped packages", async () => {
const testDir = await initFixture("basic");
await lernaBootstrap(testDir)("--scope", "package-@(3|4)");
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
expect(symlinkedDirectories(testDir)).toMatchSnapshot();
});
it("should respect --force-local", async () => {
const testDir = await initFixture("basic");
await lernaBootstrap(testDir)("--scope", "@test/package-1", "--scope", "package-4", "--force-local");
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
expect(symlinkedDirectories(testDir)).toMatchSnapshot();
});
it("should respect --force-local when a single package is in scope", async () => {
const testDir = await initFixture("basic");
await lernaBootstrap(testDir)("--scope", "package-4", "--force-local");
// no packages were installed from the registry
const installed = installedPackagesInDirectories(testDir);
expect(installed["packages/package-4"] || []).toEqual([]);
// package-3 was resolved as a local symlink
const symlinked = symlinkedDirectories(testDir);
expect(symlinked).toContainEqual({
_src: "packages/package-3",
dest: "packages/package-4/node_modules/package-3",
type: "junction",
});
});
it("should not update package.json when filtering", async () => {
const testDir = await initFixture("basic");
await lernaBootstrap(testDir)("--scope", "@test/package-2", "--ci");
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
expect(symlinkedDirectories(testDir)).toMatchSnapshot();
expect(npmInstall.dependencies.mock.calls[0][2]).toMatchObject({
subCommand: "install", // not "ci"
npmClient: "npm",
npmClientArgs: ["--no-save"],
});
});
it("should not update yarn.lock when filtering", async () => {
const testDir = await initFixture("basic");
await lernaBootstrap(testDir)("--scope", "@test/package-2", "--npm-client", "yarn", "--ci");
expect(npmInstall.dependencies.mock.calls[0][2]).toMatchObject({
npmClient: "yarn",
npmClientArgs: ["--pure-lockfile"],
});
});
it("never installs with global style", async () => {
const testDir = await initFixture("basic");
await lernaBootstrap(testDir)();
expect(npmInstall.dependencies).toHaveBeenCalledWith(
expect.objectContaining({
name: "@test/package-2",
}),
expect.arrayContaining(["foo@^1.0.0"]),
expect.objectContaining({
npmGlobalStyle: false,
})
);
});
});
describe("with multiple package locations", () => {
it("should bootstrap packages", async () => {
const testDir = await initFixture("extra");
await lernaBootstrap(testDir)();
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
expect(symlinkedDirectories(testDir)).toMatchSnapshot();
expect(runLifecycle.getOrderedCalls()).toEqual([["@test/package-1", "prepublish"]]);
});
it("should not bootstrap ignored packages", async () => {
const testDir = await initFixture("extra");
await lernaBootstrap(testDir)("--ignore", "package-@(3|4)");
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
});
it("bootstraps dependencies not included by --scope with --include-filtered-dependencies", async () => {
const testDir = await initFixture("extra");
// we scope to package-2 only but should still install package-1 as it is a dependency of package-2
await lernaBootstrap(testDir)("--scope", "package-2", "--include-filtered-dependencies");
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
});
it("bootstraps dependencies excluded by --ignore with --include-filtered-dependencies", async () => {
const testDir = await initFixture("extra");
// we ignore package 1 but it should still be installed because it is a dependency of package-2
await lernaBootstrap(testDir)(
"--ignore",
"{@test/package-1,package-@(3|4)}",
"--include-filtered-dependencies"
);
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
});
it("hoists appropriately", async () => {
const testDir = await initFixture("extra");
await lernaBootstrap(testDir)("--hoist");
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
expect(symlinkedDirectories(testDir)).toMatchSnapshot();
});
});
describe("with external dependencies that haven't been installed", () => {
it("should get installed", async () => {
const testDir = await initFixture("cold");
await lernaBootstrap(testDir)();
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
});
it("gets network mutex when --npm-client=yarn", async () => {
const testDir = await initFixture("cold");
await lernaBootstrap(testDir)("--npm-client", "yarn");
expect(npmInstall.dependencies.mock.calls[0][2]).toMatchObject({
npmClient: "yarn",
mutex: expect.stringMatching(/^network:\d+$/),
});
});
it("gets user defined mutex when --npm-client=yarn", async () => {
const testDir = await initFixture("cold");
await lernaBootstrap(testDir)("--npm-client", "yarn", "--mutex", "file:/test/this/path");
expect(npmInstall.dependencies.mock.calls[0][2]).toMatchObject({
npmClient: "yarn",
mutex: "file:/test/this/path",
});
});
it("hoists appropriately", async () => {
const testDir = await initFixture("cold");
await updateLernaConfig(testDir, {
hoist: true,
});
await lernaBootstrap(testDir)();
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
expect(symlinkedDirectories(testDir)).toMatchSnapshot();
});
});
describe("with external dependencies that have already been installed", () => {
it("should not get re-installed", async () => {
const testDir = await initFixture("warm");
await lernaBootstrap(testDir)();
expect(npmInstall.dependencies).not.toHaveBeenCalled();
});
it("hoists appropriately", async () => {
const testDir = await initFixture("warm");
await updateLernaConfig(testDir, {
command: {
bootstrap: {
hoist: true,
},
},
});
await lernaBootstrap(testDir)();
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
expect(symlinkedDirectories(testDir)).toMatchSnapshot();
});
});
describe("with at least one external dependency to install", () => {
it("should install all dependencies", async () => {
const testDir = await initFixture("tepid");
await lernaBootstrap(testDir)();
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
});
it("hoists appropriately", async () => {
const testDir = await initFixture("tepid");
await lernaBootstrap(testDir)("--hoist");
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
expect(symlinkedDirectories(testDir)).toMatchSnapshot();
});
});
describe("with package peerDependencies", () => {
it("does not bootstrap peerDependencies", async () => {
const testDir = await initFixture("peer");
await lernaBootstrap(testDir)();
expect(npmInstall.dependencies).not.toHaveBeenCalled();
});
});
describe("with registry config", () => {
it("should install packages from registry", async () => {
const testDir = await initFixture("registries");
await lernaBootstrap(testDir)();
expect(installedPackagesInDirectories(testDir)).toMatchSnapshot();
expect(npmInstall.dependencies).toHaveBeenLastCalledWith(
expect.objectContaining({
name: "@test/package-1",
}),
expect.arrayContaining(["foo@^1.0.0"]),
expect.objectContaining({
registry: "https://my-secure-registry/npm",
npmClient: "npm",
npmGlobalStyle: false,
})
);
});
});
describe("with remaining arguments", () => {
describe("by default", () => {
it("should turn it into npmClientArgs", async () => {
const testDir = await initFixture("npm-client-args-1");
await lernaBootstrap(testDir)("--", "--no-optional", "--production");
expect(npmInstall.dependencies.mock.calls[0][2]).toMatchObject({
npmClientArgs: ["--no-optional", "--production"],
});
});
});
describe("and configured npmClientArgs option", () => {
it("should merge both together", async () => {
const testDir = await initFixture("npm-client-args-2");
await lernaBootstrap(testDir)("--", "--no-optional");
expect(npmInstall.dependencies.mock.calls[0][2]).toMatchObject({
npmClientArgs: ["--production", "--no-optional"],
});
});
});
});
describe("with yarn workspaces", () => {
it("should use workspaces feature when installing", async () => {
const testDir = await initFixture("yarn-workspaces");
await lernaBootstrap(testDir)();
expect(npmInstall.dependencies).not.toHaveBeenCalled();
expect(npmInstall).toHaveBeenLastCalledWith(
expect.objectContaining({ name: "root" }),
expect.objectContaining({
npmClient: "yarn",
mutex: expect.stringMatching(/^network:\d+$/),
})
);
});
it("errors when package.json workspaces exists but --use-workspaces is not enabled", async () => {
expect.assertions(1);
const testDir = await initFixture("yarn-workspaces");
try {
await lernaBootstrap(testDir)("--no-use-workspaces");
} catch (err) {
expect(err.message).toMatch(
"Yarn workspaces are configured in package.json, but not enabled in lerna.json!"
);
}
});
});
describe("with relative file: specifiers in root dependencies", () => {
it("only installs in the root", async () => {
const testDir = await initFixture("relative-file-specs");
await lernaBootstrap(testDir)();
expect(npmInstall.dependencies).not.toHaveBeenCalled();
expect(npmInstall).toHaveBeenLastCalledWith(
expect.objectContaining({ name: "relative-file-specs" }),
expect.objectContaining({
npmClient: "npm",
stdio: "inherit",
})
);
});
});
describe("with duplicate package names", () => {
it("throws an error", async () => {
expect.assertions(1);
const testDir = await initFixture("duplicate-package-names");
try {
await lernaBootstrap(testDir)();
} catch (err) {
expect(err.message).toMatch(`Package name "package-1" used in multiple packages`);
}
});
});
describe("in a cyclical repo", () => {
it("should throw an error with --reject-cycles", async () => {
expect.assertions(1);
const testDir = await initFixture("toposort");
try {
await lernaBootstrap(testDir)("--reject-cycles");
} catch (err) {
expect(err.message).toMatch("Dependency cycles detected, you should fix these!");
}
});
});
it("succeeds in repositories with zero packages", async () => {
const testDir = await initFixture("zero-pkgs");
const result = await lernaBootstrap(testDir)();
// cheesy workaround for jest's expectation of assertions
expect(result).toBeDefined();
});
it("does not require an initialized git repo", async () => {
const testDir = await initFixture("zero-pkgs");
await fs.remove(path.join(testDir, ".git"));
const result = await lernaBootstrap(testDir)();
// cheesy workaround for jest's expectation of assertions
expect(result).toBeDefined();
});
it("requires a git repo when using --since", async () => {
expect.assertions(1);
const testDir = await initFixture("zero-pkgs");
await fs.remove(path.join(testDir, ".git"));
try {
await lernaBootstrap(testDir)("--since", "some-branch");
} catch (err) {
expect(err.message).toMatch("this is not a git repository");
}
});
describe("with force-local", () => {
it("links all packages", async () => {
const testDir = await initFixture("force-local");
await lernaBootstrap(testDir)("--force-local");
expect(symlinkedDirectories(testDir)).toMatchSnapshot();
});
});
});