-
Notifications
You must be signed in to change notification settings - Fork 964
Expand file tree
/
Copy pathdeps-graph.e2e.ts
More file actions
672 lines (650 loc) · 31.6 KB
/
Copy pathdeps-graph.e2e.ts
File metadata and controls
672 lines (650 loc) · 31.6 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
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
import fs from 'fs';
import { generateRandomStr } from '@teambit/toolbox.string.random';
import { DEPS_GRAPH } from '@teambit/harmony.modules.feature-toggle';
import { addDistTag } from '@pnpm/registry-mock';
import path from 'path';
import chai, { expect } from 'chai';
import chaiFs from 'chai-fs';
import yaml from 'js-yaml';
import { Helper, NpmCiRegistry, supportNpmCiRegistryTesting } from '@teambit/legacy.e2e-helper';
chai.use(chaiFs);
(supportNpmCiRegistryTesting ? describe : describe.skip)('dependencies graph data', function () {
this.timeout(0);
let npmCiRegistry: NpmCiRegistry;
let helper: Helper;
before(() => {
helper = new Helper();
helper.command.setFeatures(DEPS_GRAPH);
});
after(() => {
helper.scopeHelper.destroy();
helper.command.resetFeatures();
});
describe('two components with different peer dependencies', function () {
const env1DefaultPeerVersion = '16.0.0';
const env2DefaultPeerVersion = '17.0.0';
let randomStr: string;
before(async () => {
randomStr = generateRandomStr(4); // to avoid publishing the same package every time the test is running
const name = `@ci/${randomStr}.{name}`;
helper.scopeHelper.setWorkspaceWithRemoteScope();
npmCiRegistry = new NpmCiRegistry(helper);
npmCiRegistry.configureCustomNameInPackageJsonHarmony(name);
await npmCiRegistry.init();
helper.command.setConfig('registry', npmCiRegistry.getRegistryUrl());
helper.env.setCustomNewEnv(
undefined,
undefined,
{
policy: {
peers: [
{
name: 'react',
version: env1DefaultPeerVersion,
supportedRange: '^16.0.0',
},
],
},
},
false,
'custom-react/env1',
'custom-react/env1'
);
helper.env.setCustomNewEnv(
undefined,
undefined,
{
policy: {
peers: [
{
name: 'react',
version: env2DefaultPeerVersion,
supportedRange: '^17.0.0',
},
],
},
},
false,
'custom-react/env2',
'custom-react/env2'
);
helper.fixtures.populateComponents(2);
helper.extensions.workspaceJsonc.addKeyValToDependencyResolver('rootComponents', true);
await addDistTag({ package: '@pnpm.e2e/foo', version: '100.0.0', distTag: 'latest' });
await addDistTag({ package: '@pnpm.e2e/bar', version: '100.0.0', distTag: 'latest' });
helper.fs.outputFile(
`comp1/index.js`,
`const React = require("react"); require("@pnpm.e2e/foo"); // eslint-disable-line`
);
helper.fs.outputFile(
`comp2/index.js`,
`const React = require("react");const comp1 = require("@ci/${randomStr}.comp1"); require("@pnpm.e2e/bar"); // eslint-disable-line`
);
helper.extensions.addExtensionToVariant('comp1', `${helper.scopes.remote}/custom-react/env1`, {});
helper.extensions.addExtensionToVariant('comp2', `${helper.scopes.remote}/custom-react/env2`, {});
helper.extensions.addExtensionToVariant('custom-react', 'teambit.envs/env', {});
helper.workspaceJsonc.addKeyValToDependencyResolver('policy', {
dependencies: {
'@pnpm.e2e/foo': '^100.0.0',
'@pnpm.e2e/bar': '^100.0.0',
},
});
helper.command.install('--add-missing-deps');
helper.command.tagAllComponents('--skip-tests');
helper.command.export();
});
after(() => {
npmCiRegistry.destroy();
helper.command.delConfig('registry');
helper.scopeHelper.destroy();
});
it('should save dependencies graph to the model of comp1', () => {
const versionObj = helper.command.catComponent('comp1@latest');
const depsGraph = JSON.parse(helper.command.catObject(versionObj.dependenciesGraphRef));
const directDependencies = depsGraph.edges.find((edge) => edge.id === '.').neighbours;
expect(directDependencies).deep.include({
name: 'react',
specifier: '16.0.0',
id: 'react@16.0.0',
lifecycle: 'runtime',
optional: false,
});
});
let depsGraph2;
let depsGraph2DirectDeps;
let comp1Package;
it('should save dependencies graph to the model comp2', () => {
const versionObj = helper.command.catComponent('comp2@latest');
depsGraph2 = JSON.parse(helper.command.catObject(versionObj.dependenciesGraphRef));
depsGraph2DirectDeps = depsGraph2.edges.find((edge) => edge.id === '.').neighbours;
expect(depsGraph2DirectDeps).deep.include({
name: 'react',
specifier: '17.0.0',
id: 'react@17.0.0',
lifecycle: 'runtime',
optional: false,
});
});
it('should replace pending version in direct dependency', () => {
expect(depsGraph2DirectDeps).deep.include({
name: `@ci/${randomStr}.comp1`,
specifier: '*',
id: `@ci/${randomStr}.comp1@0.0.1(react@17.0.0)`,
lifecycle: 'runtime',
optional: false,
});
});
it('should update integrity of dependency component', () => {
comp1Package = depsGraph2.packages[`@ci/${randomStr}.comp1@0.0.1`];
expect(comp1Package.resolution.integrity).to.match(/^sha512-/);
});
it('should add component ID to the deps graph', () => {
expect(comp1Package.component).to.eql({ scope: helper.scopes.remote, name: 'comp1' });
});
describe('importing a component that depends on another component and was export together with that component', () => {
before(async () => {
helper.scopeHelper.reInitWorkspace();
helper.scopeHelper.addRemoteScope();
await addDistTag({ package: '@pnpm.e2e/foo', version: '100.1.0', distTag: 'latest' });
await addDistTag({ package: '@pnpm.e2e/bar', version: '100.1.0', distTag: 'latest' });
helper.command.import(`${helper.scopes.remote}/comp2@latest`);
});
let lockfile: any;
it('should generate a lockfile', () => {
lockfile = yaml.load(fs.readFileSync(path.join(helper.scopes.localPath, 'pnpm-lock.yaml'), 'utf8'));
expect(lockfile.bit.restoredFromModel).to.eq(true);
});
it('should import the component with its own resolved versions', () => {
expect(lockfile.packages).to.not.have.property('@pnpm.e2e/foo@100.1.0');
expect(lockfile.packages).to.not.have.property('@pnpm.e2e/bar@100.1.0');
expect(lockfile.packages).to.have.property('@pnpm.e2e/foo@100.0.0');
expect(lockfile.packages).to.have.property('@pnpm.e2e/bar@100.0.0');
});
});
});
describe('two components exported with different peer dependencies using the same env', function () {
let randomStr: string;
before(async () => {
randomStr = generateRandomStr(4); // to avoid publishing the same package every time the test is running
const name = `@ci/${randomStr}.{name}`;
helper.scopeHelper.setWorkspaceWithRemoteScope();
npmCiRegistry = new NpmCiRegistry(helper);
npmCiRegistry.configureCustomNameInPackageJsonHarmony(name);
await npmCiRegistry.init();
helper.command.setConfig('registry', npmCiRegistry.getRegistryUrl());
helper.env.setCustomNewEnv(
undefined,
undefined,
{
policy: {
peers: [
{
name: '@pnpm.e2e/abc',
version: '*',
supportedRange: '*',
},
],
},
},
false,
'custom-env/env',
'custom-env/env'
);
helper.fs.createFile('bar', 'bar.js', 'require("@pnpm.e2e/abc"); // eslint-disable-line');
helper.command.addComponent('bar');
helper.extensions.addExtensionToVariant('bar', `${helper.scopes.remote}/custom-env/env`, {});
await addDistTag({ package: '@pnpm.e2e/abc', version: '1.0.0', distTag: 'latest' });
await addDistTag({ package: '@pnpm.e2e/peer-a', version: '1.0.1', distTag: 'latest' });
helper.command.install('--add-missing-deps');
helper.command.tagAllComponents('--skip-tests');
helper.command.export();
await addDistTag({ package: '@pnpm.e2e/abc', version: '2.0.0', distTag: 'latest' });
await addDistTag({ package: '@pnpm.e2e/peer-a', version: '1.0.0', distTag: 'latest' });
helper.scopeHelper.reInitWorkspace();
helper.scopeHelper.addRemoteScope();
helper.fs.createFile('foo', 'foo.js', `require("@pnpm.e2e/abc"); require("@ci/${randomStr}.bar");`);
helper.command.addComponent('foo');
helper.extensions.addExtensionToVariant('foo', `${helper.scopes.remote}/custom-env/env@0.0.1`, {});
helper.command.install('--add-missing-deps');
helper.command.snapAllComponentsWithoutBuild('--skip-tests');
helper.command.export();
helper.scopeHelper.reInitWorkspace();
helper.scopeHelper.addRemoteScope();
helper.command.import(`${helper.scopes.remote}/foo@latest ${helper.scopes.remote}/bar@latest`);
});
let lockfile: any;
it('should generate a lockfile', () => {
lockfile = yaml.load(fs.readFileSync(path.join(helper.scopes.localPath, 'pnpm-lock.yaml'), 'utf8'));
expect(lockfile.bit.restoredFromModel).to.eq(true);
});
it('should resolve to one version of the peer dependency, the highest one', () => {
expect(lockfile.packages).to.not.have.property('@pnpm.e2e/peer-a@1.0.0');
expect(lockfile.packages).to.not.have.property('@pnpm.e2e/abc@1.0.0');
expect(lockfile.packages).to.have.property('@pnpm.e2e/peer-a@1.0.1');
expect(lockfile.packages).to.have.property('@pnpm.e2e/abc@2.0.0');
});
it('imported component is not installed as a dependency', () => {
expect(lockfile.packages).to.not.have.property(`@ci/${randomStr}.bar@0.0.1`);
});
after(() => {
npmCiRegistry.destroy();
helper.command.delConfig('registry');
helper.scopeHelper.destroy();
});
});
// Covers the "reimport drift" path: when a second component is imported into a
// workspace that already has installed components, the graph-generated lockfile must
// not overwrite pnpm-lock.yaml with only the newly-imported component's subgraph — if
// it did, existing workspace dependencies would be re-resolved by pnpm against the
// manifest specifiers and drift to newer registry versions.
describe('importing a component into a workspace that already has an installed component', function () {
let randomStr: string;
let initialLockfile: any;
let lockfileAfterSecondImport: any;
before(async () => {
randomStr = generateRandomStr(4);
const name = `@ci/${randomStr}.{name}`;
helper.scopeHelper.setWorkspaceWithRemoteScope();
npmCiRegistry = new NpmCiRegistry(helper);
npmCiRegistry.configureCustomNameInPackageJsonHarmony(name);
await npmCiRegistry.init();
helper.command.setConfig('registry', npmCiRegistry.getRegistryUrl());
helper.env.setCustomNewEnv(
undefined,
undefined,
{ policy: { peers: [] } },
false,
'custom-env/env',
'custom-env/env'
);
helper.fs.createFile('comp1', 'comp1.js', 'require("@pnpm.e2e/foo"); // eslint-disable-line');
helper.command.addComponent('comp1');
helper.extensions.addExtensionToVariant('comp1', `${helper.scopes.remote}/custom-env/env`, {});
helper.fs.createFile('comp2', 'comp2.js', 'require("@pnpm.e2e/bar"); // eslint-disable-line');
helper.command.addComponent('comp2');
helper.extensions.addExtensionToVariant('comp2', `${helper.scopes.remote}/custom-env/env`, {});
helper.extensions.workspaceJsonc.addKeyValToDependencyResolver('rootComponents', true);
await addDistTag({ package: '@pnpm.e2e/foo', version: '100.0.0', distTag: 'latest' });
await addDistTag({ package: '@pnpm.e2e/bar', version: '100.0.0', distTag: 'latest' });
helper.command.install('--add-missing-deps');
helper.command.tagAllComponents('--skip-tests');
helper.command.export();
helper.scopeHelper.reInitWorkspace();
helper.scopeHelper.addRemoteScope();
helper.extensions.workspaceJsonc.addKeyValToDependencyResolver('rootComponents', true);
helper.command.import(`${helper.scopes.remote}/comp1@latest`);
initialLockfile = yaml.load(fs.readFileSync(path.join(helper.scopes.localPath, 'pnpm-lock.yaml'), 'utf8'));
await addDistTag({ package: '@pnpm.e2e/foo', version: '100.1.0', distTag: 'latest' });
helper.command.import(`${helper.scopes.remote}/comp2@latest`);
lockfileAfterSecondImport = yaml.load(
fs.readFileSync(path.join(helper.scopes.localPath, 'pnpm-lock.yaml'), 'utf8')
);
});
after(() => {
npmCiRegistry.destroy();
helper.command.delConfig('registry');
helper.scopeHelper.destroy();
});
it('first import should restore the lockfile from comp1 graph', () => {
expect(initialLockfile.bit.restoredFromModel).to.eq(true);
expect(initialLockfile.packages).to.have.property('@pnpm.e2e/foo@100.0.0');
});
it('second import should include comp2 deps at the versions stored in its graph', () => {
expect(lockfileAfterSecondImport.packages).to.have.property('@pnpm.e2e/bar@100.0.0');
});
// Regression coverage: graph-based lockfile regeneration previously overwrote
// pnpm-lock.yaml with only comp2's subgraph, causing foo to be re-resolved from the
// manifest specifier and drift to the newer registry version.
it('second import should preserve comp1 deps at their previously-locked versions', () => {
expect(lockfileAfterSecondImport.packages).to.have.property('@pnpm.e2e/foo@100.0.0');
expect(lockfileAfterSecondImport.packages).to.not.have.property('@pnpm.e2e/foo@100.1.0');
});
});
// Same class of drift as the previous block, but for the "pull updated version of an
// already-imported component" path. Only the re-imported component's IDs are passed to
// installPackagesGracefully, so only its graph is used to regenerate the lockfile — the
// merge helper has to preserve unrelated components' previously-locked deps.
describe('re-importing an updated version of an already-imported component', function () {
let randomStr: string;
let lockfileAfterReimport: any;
before(async () => {
randomStr = generateRandomStr(4);
const name = `@ci/${randomStr}.{name}`;
helper.scopeHelper.setWorkspaceWithRemoteScope();
npmCiRegistry = new NpmCiRegistry(helper);
npmCiRegistry.configureCustomNameInPackageJsonHarmony(name);
await npmCiRegistry.init();
helper.command.setConfig('registry', npmCiRegistry.getRegistryUrl());
helper.env.setCustomNewEnv(
undefined,
undefined,
{ policy: { peers: [] } },
false,
'custom-env/env',
'custom-env/env'
);
helper.fs.createFile('comp1', 'comp1.js', 'require("@pnpm.e2e/foo"); // eslint-disable-line');
helper.command.addComponent('comp1');
helper.extensions.addExtensionToVariant('comp1', `${helper.scopes.remote}/custom-env/env`, {});
helper.fs.createFile('comp2', 'comp2.js', 'require("@pnpm.e2e/bar"); // eslint-disable-line');
helper.command.addComponent('comp2');
helper.extensions.addExtensionToVariant('comp2', `${helper.scopes.remote}/custom-env/env`, {});
helper.extensions.workspaceJsonc.addKeyValToDependencyResolver('rootComponents', true);
await addDistTag({ package: '@pnpm.e2e/foo', version: '100.0.0', distTag: 'latest' });
await addDistTag({ package: '@pnpm.e2e/bar', version: '100.0.0', distTag: 'latest' });
helper.command.install('--add-missing-deps');
helper.command.tagAllComponents('--skip-tests');
helper.command.export();
helper.fs.appendFile('comp1/comp1.js', '\nmodule.exports = 1;');
helper.command.tagAllComponents('--skip-tests --unmodified');
helper.command.export();
helper.scopeHelper.reInitWorkspace();
helper.scopeHelper.addRemoteScope();
helper.extensions.workspaceJsonc.addKeyValToDependencyResolver('rootComponents', true);
helper.command.import(`${helper.scopes.remote}/comp1@0.0.1 ${helper.scopes.remote}/comp2@latest`);
await addDistTag({ package: '@pnpm.e2e/bar', version: '100.1.0', distTag: 'latest' });
helper.command.import(`${helper.scopes.remote}/comp1@latest`);
lockfileAfterReimport = yaml.load(
fs.readFileSync(path.join(helper.scopes.localPath, 'pnpm-lock.yaml'), 'utf8')
);
});
after(() => {
npmCiRegistry.destroy();
helper.command.delConfig('registry');
helper.scopeHelper.destroy();
});
// Regression coverage: re-importing comp1 must not regenerate the lockfile from
// comp1's graph only and drop comp2's bar entry. Otherwise pnpm may re-resolve bar
// from the manifest specifier and drift to the newer registry version.
it('should preserve comp2 dependency versions that are unrelated to the re-imported component', () => {
expect(lockfileAfterReimport.packages).to.have.property('@pnpm.e2e/bar@100.0.0');
expect(lockfileAfterReimport.packages).to.not.have.property('@pnpm.e2e/bar@100.1.0');
});
});
describe('three components sharing a peer dependency', function () {
let randomStr: string;
let lockfile: any;
before(async () => {
randomStr = generateRandomStr(4);
const name = `@ci/${randomStr}.{name}`;
helper.scopeHelper.setWorkspaceWithRemoteScope();
npmCiRegistry = new NpmCiRegistry(helper);
npmCiRegistry.configureCustomNameInPackageJsonHarmony(name);
await npmCiRegistry.init();
helper.command.setConfig('registry', npmCiRegistry.getRegistryUrl());
helper.env.setCustomNewEnv(
undefined,
undefined,
{
policy: {
peers: [
{
name: '@pnpm.e2e/abc',
version: '*',
supportedRange: '*',
},
],
},
},
false,
'custom-env/env',
'custom-env/env'
);
helper.fs.createFile('bar', 'bar.js', 'require("@pnpm.e2e/abc"); // eslint-disable-line');
helper.command.addComponent('bar');
helper.extensions.addExtensionToVariant('bar', `${helper.scopes.remote}/custom-env/env`, {});
await addDistTag({ package: '@pnpm.e2e/abc', version: '1.0.0', distTag: 'latest' });
await addDistTag({ package: '@pnpm.e2e/peer-a', version: '1.0.0', distTag: 'latest' });
helper.command.install('--add-missing-deps');
helper.command.tagAllComponents('--skip-tests');
helper.command.export();
await addDistTag({ package: '@pnpm.e2e/abc', version: '2.0.0', distTag: 'latest' });
await addDistTag({ package: '@pnpm.e2e/peer-a', version: '1.0.1', distTag: 'latest' });
helper.scopeHelper.reInitWorkspace();
helper.scopeHelper.addRemoteScope();
helper.fs.createFile('foo', 'foo.js', `require("@pnpm.e2e/abc"); require("@ci/${randomStr}.bar");`);
helper.command.addComponent('foo');
helper.extensions.addExtensionToVariant('foo', `${helper.scopes.remote}/custom-env/env@0.0.1`, {});
helper.command.install('--add-missing-deps');
helper.command.snapAllComponentsWithoutBuild('--skip-tests');
helper.command.export();
// A third component re-uses one of the already-published versions (peer-a@1.0.0)
// so the merge has to reconcile three graphs where two of them agree on the lower
// version and one carries the higher version.
await addDistTag({ package: '@pnpm.e2e/abc', version: '1.0.0', distTag: 'latest' });
await addDistTag({ package: '@pnpm.e2e/peer-a', version: '1.0.0', distTag: 'latest' });
helper.scopeHelper.reInitWorkspace();
helper.scopeHelper.addRemoteScope();
helper.fs.createFile('baz', 'baz.js', `require("@pnpm.e2e/abc"); require("@ci/${randomStr}.bar");`);
helper.command.addComponent('baz');
helper.extensions.addExtensionToVariant('baz', `${helper.scopes.remote}/custom-env/env@0.0.1`, {});
helper.command.install('--add-missing-deps');
helper.command.snapAllComponentsWithoutBuild('--skip-tests');
helper.command.export();
helper.scopeHelper.reInitWorkspace();
helper.scopeHelper.addRemoteScope();
helper.command.import(
`${helper.scopes.remote}/foo@latest ${helper.scopes.remote}/bar@latest ${helper.scopes.remote}/baz@latest`
);
lockfile = yaml.load(fs.readFileSync(path.join(helper.scopes.localPath, 'pnpm-lock.yaml'), 'utf8'));
});
after(() => {
npmCiRegistry.destroy();
helper.command.delConfig('registry');
helper.scopeHelper.destroy();
});
it('should restore the lockfile from the merged graphs', () => {
expect(lockfile.bit.restoredFromModel).to.eq(true);
});
// The root edge of the merged graph picks the highest peer version per (name, specifier),
// so the highest variant must be present in the lockfile.
it('should include the highest version of the shared peer dependency across all three graphs', () => {
expect(lockfile.packages).to.have.property('@pnpm.e2e/abc@2.0.0');
expect(lockfile.packages).to.have.property('@pnpm.e2e/peer-a@1.0.1');
});
// Documents a known limitation of the merge: highest-wins is applied only to the root
// edge. Transitive snapshots from lower-version graphs still reference the older
// versions of non-peer packages, so pnpm retains them in the lockfile. (Peer versions
// get reconciled across the workspace at install time, so they don't leak; regular
// dependencies do.)
it('currently leaves the lower version of non-peer transitives in the lockfile', () => {
expect(lockfile.packages).to.have.property('@pnpm.e2e/abc@1.0.0');
});
});
// Regression coverage for devDependencies + optionalDependencies round-trip through
// the graph. The graph edge neighbours carry `lifecycle` and `optional` flags; these
// must make it back into the regenerated lockfile's importer entries and snapshots.
describe('dev and optional dependencies round-trip through the graph', function () {
let randomStr: string;
let depsGraph: any;
let lockfile: any;
before(async () => {
randomStr = generateRandomStr(4);
const name = `@ci/${randomStr}.{name}`;
helper.scopeHelper.setWorkspaceWithRemoteScope();
npmCiRegistry = new NpmCiRegistry(helper);
npmCiRegistry.configureCustomNameInPackageJsonHarmony(name);
await npmCiRegistry.init();
helper.command.setConfig('registry', npmCiRegistry.getRegistryUrl());
helper.env.setCustomNewEnv(
undefined,
undefined,
{
policy: {
dev: [
{
name: '@pnpm.e2e/foo',
version: '100.0.0',
hidden: true,
force: true,
},
],
},
},
false,
'custom-env/env',
'custom-env/env'
);
helper.fs.createFile('comp1', 'comp1.js', 'require("@pnpm.e2e/bar"); // eslint-disable-line');
helper.command.addComponent('comp1');
helper.extensions.addExtensionToVariant('comp1', `${helper.scopes.remote}/custom-env/env`, {});
helper.extensions.workspaceJsonc.addKeyValToDependencyResolver('rootComponents', true);
await addDistTag({ package: '@pnpm.e2e/foo', version: '100.0.0', distTag: 'latest' });
await addDistTag({ package: '@pnpm.e2e/bar', version: '100.0.0', distTag: 'latest' });
helper.command.install('--add-missing-deps');
helper.command.tagAllComponents('--skip-tests');
helper.command.export();
const versionObj = helper.command.catComponent('comp1@latest');
depsGraph = JSON.parse(helper.command.catObject(versionObj.dependenciesGraphRef));
helper.scopeHelper.reInitWorkspace();
helper.scopeHelper.addRemoteScope();
helper.extensions.workspaceJsonc.addKeyValToDependencyResolver('rootComponents', true);
helper.command.import(`${helper.scopes.remote}/comp1@latest`);
lockfile = yaml.load(fs.readFileSync(path.join(helper.scopes.localPath, 'pnpm-lock.yaml'), 'utf8'));
});
after(() => {
npmCiRegistry.destroy();
helper.command.delConfig('registry');
helper.scopeHelper.destroy();
});
it('should record the dev-only dependency in the graph with lifecycle=dev', () => {
const directDependencies = depsGraph.edges.find((edge) => edge.id === '.').neighbours;
const fooDep = directDependencies.find((dep) => dep.name === '@pnpm.e2e/foo');
expect(fooDep, 'foo should be a direct dependency in the graph').to.exist;
expect(fooDep.lifecycle).to.eq('dev');
});
it('should record the runtime dependency in the graph with lifecycle=runtime', () => {
const directDependencies = depsGraph.edges.find((edge) => edge.id === '.').neighbours;
const barDep = directDependencies.find((dep) => dep.name === '@pnpm.e2e/bar');
expect(barDep, 'bar should be a direct dependency in the graph').to.exist;
expect(barDep.lifecycle).to.eq('runtime');
});
it('should restore the lockfile from the graph', () => {
expect(lockfile.bit.restoredFromModel).to.eq(true);
expect(lockfile.packages).to.have.property('@pnpm.e2e/foo@100.0.0');
expect(lockfile.packages).to.have.property('@pnpm.e2e/bar@100.0.0');
});
});
// `bit install --restore` seeds the lockfile from the dependency graphs stored on
// every bitmap entry, the same way `bit import` does for the components it writes.
// This lets a user recover from a deleted pnpm-lock.yaml without re-resolving from
// manifest specifiers (which would drift to whatever the registry considers latest).
describe('bit install --restore rebuilds the lockfile from workspace component graphs', function () {
let randomStr: string;
let lockfileAfterRestore: any;
before(async () => {
randomStr = generateRandomStr(4);
const name = `@ci/${randomStr}.{name}`;
helper.scopeHelper.setWorkspaceWithRemoteScope();
npmCiRegistry = new NpmCiRegistry(helper);
npmCiRegistry.configureCustomNameInPackageJsonHarmony(name);
await npmCiRegistry.init();
helper.command.setConfig('registry', npmCiRegistry.getRegistryUrl());
helper.env.setCustomNewEnv(
undefined,
undefined,
{ policy: { peers: [] } },
false,
'custom-env/env',
'custom-env/env'
);
helper.fs.createFile('comp1', 'comp1.js', 'require("@pnpm.e2e/foo"); // eslint-disable-line');
helper.command.addComponent('comp1');
helper.extensions.addExtensionToVariant('comp1', `${helper.scopes.remote}/custom-env/env`, {});
helper.fs.createFile('comp2', 'comp2.js', 'require("@pnpm.e2e/bar"); // eslint-disable-line');
helper.command.addComponent('comp2');
helper.extensions.addExtensionToVariant('comp2', `${helper.scopes.remote}/custom-env/env`, {});
helper.extensions.workspaceJsonc.addKeyValToDependencyResolver('rootComponents', true);
await addDistTag({ package: '@pnpm.e2e/foo', version: '100.0.0', distTag: 'latest' });
await addDistTag({ package: '@pnpm.e2e/bar', version: '100.0.0', distTag: 'latest' });
helper.command.install('--add-missing-deps');
helper.command.tagAllComponents('--skip-tests');
helper.command.export();
helper.scopeHelper.reInitWorkspace();
helper.scopeHelper.addRemoteScope();
helper.extensions.workspaceJsonc.addKeyValToDependencyResolver('rootComponents', true);
helper.command.import(`${helper.scopes.remote}/comp1@latest ${helper.scopes.remote}/comp2@latest`);
// bump registry and blow away the lockfile + node_modules, then restore from graphs.
await addDistTag({ package: '@pnpm.e2e/foo', version: '100.1.0', distTag: 'latest' });
await addDistTag({ package: '@pnpm.e2e/bar', version: '100.1.0', distTag: 'latest' });
helper.fs.deletePath('pnpm-lock.yaml');
helper.fs.deletePath('node_modules');
helper.command.runCmd('bit install --restore');
lockfileAfterRestore = yaml.load(fs.readFileSync(path.join(helper.scopes.localPath, 'pnpm-lock.yaml'), 'utf8'));
});
after(() => {
npmCiRegistry.destroy();
helper.command.delConfig('registry');
helper.scopeHelper.destroy();
});
it('should mark the regenerated lockfile as restoredFromModel', () => {
expect(lockfileAfterRestore.bit.restoredFromModel).to.eq(true);
});
it('should keep both components locked to the versions stored in their graphs', () => {
expect(lockfileAfterRestore.packages).to.have.property('@pnpm.e2e/foo@100.0.0');
expect(lockfileAfterRestore.packages).to.have.property('@pnpm.e2e/bar@100.0.0');
expect(lockfileAfterRestore.packages).to.not.have.property('@pnpm.e2e/foo@100.1.0');
expect(lockfileAfterRestore.packages).to.not.have.property('@pnpm.e2e/bar@100.1.0');
});
});
// --restore is an explicit opt-in, so it has to bypass the DEPS_GRAPH feature toggle
// and work on workspaces that never enabled the flag. The graph itself was authored on
// a scope that had the flag on at tag time, but the consumer shouldn't need to flip
// the flag just to restore from it.
describe('bit install --restore works when the DEPS_GRAPH feature toggle is disabled', function () {
let randomStr: string;
let lockfileAfterRestore: any;
before(async () => {
randomStr = generateRandomStr(4);
const name = `@ci/${randomStr}.{name}`;
helper.scopeHelper.setWorkspaceWithRemoteScope();
npmCiRegistry = new NpmCiRegistry(helper);
npmCiRegistry.configureCustomNameInPackageJsonHarmony(name);
await npmCiRegistry.init();
helper.command.setConfig('registry', npmCiRegistry.getRegistryUrl());
helper.env.setCustomNewEnv(
undefined,
undefined,
{ policy: { peers: [] } },
false,
'custom-env/env',
'custom-env/env'
);
helper.fs.createFile('comp1', 'comp1.js', 'require("@pnpm.e2e/foo"); // eslint-disable-line');
helper.command.addComponent('comp1');
helper.extensions.addExtensionToVariant('comp1', `${helper.scopes.remote}/custom-env/env`, {});
helper.extensions.workspaceJsonc.addKeyValToDependencyResolver('rootComponents', true);
await addDistTag({ package: '@pnpm.e2e/foo', version: '100.0.0', distTag: 'latest' });
helper.command.install('--add-missing-deps');
helper.command.tagAllComponents('--skip-tests');
helper.command.export();
helper.scopeHelper.reInitWorkspace();
helper.scopeHelper.addRemoteScope();
helper.extensions.workspaceJsonc.addKeyValToDependencyResolver('rootComponents', true);
// Turn the DEPS_GRAPH feature toggle off for the remainder of the test. The outer
// describe's after() hook will restore it via resetFeatures, but we also restore it
// here so subsequent describes in this suite aren't affected.
helper.command.resetFeatures();
try {
helper.command.import(`${helper.scopes.remote}/comp1@latest`);
await addDistTag({ package: '@pnpm.e2e/foo', version: '100.1.0', distTag: 'latest' });
helper.fs.deletePath('pnpm-lock.yaml');
helper.fs.deletePath('node_modules');
helper.command.runCmd('bit install --restore');
lockfileAfterRestore = yaml.load(fs.readFileSync(path.join(helper.scopes.localPath, 'pnpm-lock.yaml'), 'utf8'));
} finally {
helper.command.setFeatures(DEPS_GRAPH);
}
});
after(() => {
npmCiRegistry.destroy();
helper.command.delConfig('registry');
helper.scopeHelper.destroy();
});
it('should still restore the lockfile from the stored graph', () => {
expect(lockfileAfterRestore.bit.restoredFromModel).to.eq(true);
expect(lockfileAfterRestore.packages).to.have.property('@pnpm.e2e/foo@100.0.0');
expect(lockfileAfterRestore.packages).to.not.have.property('@pnpm.e2e/foo@100.1.0');
});
});
});