-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
list-command.test.js
396 lines (347 loc) · 12 KB
/
list-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
"use strict";
// mocked modules
const output = require("@lerna/output");
// helpers
const initFixture = require("@lerna-test/init-fixture")(__dirname);
// file under test
const lernaLs = require("@lerna-test/command-runner")(require("../command"));
// remove quotes around top-level strings
expect.addSnapshotSerializer({
test(val) {
return typeof val === "string";
},
serialize(val, config, indentation, depth) {
// top-level strings don't need quotes, but nested ones do (object properties, etc)
return depth ? `"${val}"` : val;
},
});
// normalize temp directory paths in snapshots
expect.addSnapshotSerializer(require("@lerna-test/serialize-windows-paths"));
expect.addSnapshotSerializer(require("@lerna-test/serialize-tempdir"));
describe("lerna ls", () => {
describe("in a basic repo", () => {
let testDir;
beforeAll(async () => {
testDir = await initFixture("basic");
});
it("should list public packages", async () => {
await lernaLs(testDir)();
expect(output.logged()).toMatchInlineSnapshot(`
package-1
package-2
package-3
package-4
`);
});
it("should also list private packages with --all", async () => {
await lernaLs(testDir)("--all");
expect(output.logged()).toMatchInlineSnapshot(`
package-1
package-2
package-3
package-4
package-5 (PRIVATE)
`);
});
it("lists public package versions and relative paths with --long", async () => {
await lernaLs(testDir)("--long");
expect(output.logged()).toMatchInlineSnapshot(`
package-1 v1.0.0 packages/package-1
package-2 v1.0.0 packages/package-2
package-3 v1.0.0 packages/package-3
package-4 v1.0.0 packages/package-4
`);
});
it("lists all package versions and relative paths with --long --all", async () => {
await lernaLs(testDir)("-la");
expect(output.logged()).toMatchInlineSnapshot(`
package-1 v1.0.0 packages/package-1
package-2 v1.0.0 packages/package-2
package-3 v1.0.0 packages/package-3
package-4 v1.0.0 packages/package-4
package-5 v1.0.0 packages/package-5 (PRIVATE)
`);
});
it("lists public package locations with --parseable", async () => {
await lernaLs(testDir)("--parseable");
expect(output.logged()).toMatchInlineSnapshot(`
__TEST_ROOTDIR__/packages/package-1
__TEST_ROOTDIR__/packages/package-2
__TEST_ROOTDIR__/packages/package-3
__TEST_ROOTDIR__/packages/package-4
`);
});
it("lists all package locations with --parseable --all", async () => {
await lernaLs(testDir)("-pa");
expect(output.logged()).toMatchInlineSnapshot(`
__TEST_ROOTDIR__/packages/package-1
__TEST_ROOTDIR__/packages/package-2
__TEST_ROOTDIR__/packages/package-3
__TEST_ROOTDIR__/packages/package-4
__TEST_ROOTDIR__/packages/package-5
`);
});
it("lists public package locations with --parseable --long", async () => {
await lernaLs(testDir)("--parseable", "--long");
expect(output.logged()).toMatchInlineSnapshot(`
__TEST_ROOTDIR__/packages/package-1:package-1:1.0.0
__TEST_ROOTDIR__/packages/package-2:package-2:1.0.0
__TEST_ROOTDIR__/packages/package-3:package-3:1.0.0
__TEST_ROOTDIR__/packages/package-4:package-4:1.0.0
`);
});
it("lists all package locations with --parseable --long --all", async () => {
await lernaLs(testDir)("-pal");
expect(output.logged()).toMatchInlineSnapshot(`
__TEST_ROOTDIR__/packages/package-1:package-1:1.0.0
__TEST_ROOTDIR__/packages/package-2:package-2:1.0.0
__TEST_ROOTDIR__/packages/package-3:package-3:1.0.0
__TEST_ROOTDIR__/packages/package-4:package-4:1.0.0
__TEST_ROOTDIR__/packages/package-5:package-5:1.0.0:PRIVATE
`);
});
it("lists packages matching --scope", async () => {
await lernaLs(testDir)("--scope", "package-1");
expect(output.logged()).toMatchInlineSnapshot(`package-1`);
});
it("does not list packages matching --ignore", async () => {
await lernaLs(testDir)("--ignore", "package-@(2|3|4|5)");
expect(output.logged()).toMatchInlineSnapshot(`package-1`);
});
it("does not list private packages with --no-private", async () => {
await lernaLs(testDir)("--no-private");
expect(output.logged()).not.toMatch("package-5 v1.0.0 (private)");
});
});
describe("in a repo with packages outside of packages/", () => {
it("should list packages", async () => {
const testDir = await initFixture("extra");
await lernaLs(testDir)();
expect(output.logged()).toMatchInlineSnapshot(`
package-3
package-1
package-2
`);
});
});
describe("--include-filtered-dependencies", () => {
it("should list packages, including filtered ones", async () => {
const testDir = await initFixture("include-filtered-dependencies");
await lernaLs(testDir)("--scope", "@test/package-2", "--include-filtered-dependencies");
expect(output.logged()).toMatchInlineSnapshot(`
@test/package-2
@test/package-1
`);
});
});
describe("--include-filtered-dependents", () => {
it("should list packages, including filtered ones", async () => {
const testDir = await initFixture("include-filtered-dependencies");
await lernaLs(testDir)("--scope", "@test/package-1", "--include-filtered-dependents");
expect(output.logged()).toMatchInlineSnapshot(`
@test/package-1
@test/package-2
`);
});
});
describe("with an undefined version", () => {
it("replaces version with MISSING", async () => {
const testDir = await initFixture("undefined-version");
await lernaLs(testDir)("--long");
expect(output.logged()).toMatchInlineSnapshot(`package-1 MISSING packages/package-1`);
});
it("appends MISSING flag to long parseable output", async () => {
const testDir = await initFixture("undefined-version");
await lernaLs(testDir)("--long", "--parseable");
expect(output.logged()).toMatchInlineSnapshot(`__TEST_ROOTDIR__/packages/package-1:package-1:MISSING`);
});
});
describe("--json", () => {
it("should list packages as json objects", async () => {
const testDir = await initFixture("basic");
await lernaLs(testDir)("--json", "-a");
// Output should be a parseable string
const jsonOutput = JSON.parse(output.logged());
expect(jsonOutput).toEqual([
{
location: expect.stringContaining("package-1"),
name: "package-1",
private: false,
version: "1.0.0",
},
{
location: expect.stringContaining("package-2"),
name: "package-2",
private: false,
version: "1.0.0",
},
{
location: expect.stringContaining("package-3"),
name: "package-3",
private: false,
version: "1.0.0",
},
{
location: expect.stringContaining("package-4"),
name: "package-4",
private: false,
version: "1.0.0",
},
{
location: expect.stringContaining("package-5"),
name: "package-5",
private: true,
version: "1.0.0",
},
]);
});
});
describe("in a Yarn workspace", () => {
it("should use package.json/workspaces setting", async () => {
const testDir = await initFixture("yarn-workspaces");
await lernaLs(testDir)();
expect(output.logged()).toMatchInlineSnapshot(`
package-1
package-2
`);
});
});
describe("with terribly complicated dependency cycles", () => {
// for reference: 1->2, 1->3, 1->4, 2->4, 2->5, 3->4, 3->6, 4->1, 4->4, 5->4, 6->4, 7->4
// We design the package tree in a very specific way. We want to test several different things
// * A package depending on itself isn't added twice (package 4)
// * A package being added twice in the same stage of the expansion isn't added twice (package 4)
// * A package that has already been processed wont get added twice (package 1)
it("should list all packages with no repeats", async () => {
const testDir = await initFixture("cycles-and-repeated-deps");
await lernaLs(testDir)("--scope", "package-1", "--include-filtered-dependencies");
// should follow all transitive deps and pass all packages except 7 with no repeats
expect(output.logged()).toMatchInlineSnapshot(`
package-1
package-2
package-3
package-4
package-5
package-6
`);
});
});
describe("with fancy 'packages' configuration", () => {
it("lists globstar-nested packages", async () => {
const testDir = await initFixture("globstar");
await lernaLs(testDir)();
expect(output.logged()).toMatchInlineSnapshot(`
globstar
package-2
package-4
package-1
package-3
package-5
`);
});
it("lists packages under explicitly configured node_modules directories", async () => {
const testDir = await initFixture("explicit-node-modules");
await lernaLs(testDir)();
expect(output.logged()).toMatchInlineSnapshot(`
alle-pattern-root
package-1
package-2
package-3
package-4
@scoped/package-5
`);
});
it("throws an error when globstars and explicit node_modules configs are mixed", async () => {
expect.assertions(1);
const testDir = await initFixture("mixed-globstar");
try {
await lernaLs(testDir)();
} catch (err) {
expect(err.message).toMatch("An explicit node_modules package path does not allow globstars");
}
});
});
describe("filtering", () => {
let testDir;
beforeAll(async () => {
testDir = await initFixture("filtering");
});
it("includes all packages when --scope is omitted", async () => {
await lernaLs(testDir)();
expect(output.logged()).toMatchInlineSnapshot(`
package-3
package-4
package-a-1
package-a-2
`);
});
it("includes packages when --scope is a package name", async () => {
await lernaLs(testDir)("--scope", "package-3");
expect(output.logged()).toMatchInlineSnapshot(`package-3`);
});
it("excludes packages when --ignore is a package name", async () => {
await lernaLs(testDir)("--ignore", "package-3");
expect(output.logged()).toMatchInlineSnapshot(`
package-4
package-a-1
package-a-2
`);
});
it("includes packages when --scope is a glob", async () => {
await lernaLs(testDir)("--scope", "package-a-*");
expect(output.logged()).toMatchInlineSnapshot(`
package-a-1
package-a-2
`);
});
it("excludes packages when --ignore is a glob", async () => {
await lernaLs(testDir)("--ignore", "package-@(2|3|4)");
expect(output.logged()).toMatchInlineSnapshot(`
package-a-1
package-a-2
`);
});
it("excludes packages when --ignore is a brace-expanded list", async () => {
await lernaLs(testDir)("--ignore", "package-{3,4}");
expect(output.logged()).toMatchInlineSnapshot(`
package-a-1
package-a-2
`);
});
it("filters packages when both --scope and --ignore are passed", async () => {
await lernaLs(testDir)("--scope", "package-a-*", "--ignore", "package-a-2");
expect(output.logged()).toMatchInlineSnapshot(`package-a-1`);
});
it("throws an error when --scope is lacking an argument", async () => {
try {
await lernaLs(testDir)("--scope");
} catch (err) {
expect(err).toHaveProperty("message", expect.stringContaining("Not enough arguments"));
}
});
it("throws an error when --scope glob excludes all packages", async () => {
expect.assertions(1);
try {
await lernaLs(testDir)("--scope", "no-package-*");
} catch (err) {
expect(err.message).toBe("No packages remain after filtering [ 'no-package-*' ]");
}
});
it("throws an error when --ignore glob excludes all packages", async () => {
expect.assertions(1);
try {
await lernaLs(testDir)("--ignore", "package-*");
} catch (err) {
expect(err.message).toBe("No packages remain after filtering [ '**', '!package-*' ]");
}
});
it("throws an error when --scope and --ignore globs exclude all packages", async () => {
expect.assertions(1);
try {
await lernaLs(testDir)("--scope", "package-a-*", "--ignore", "package-a-@(1|2)");
} catch (err) {
expect(err.message).toBe("No packages remain after filtering [ 'package-a-*', '!package-a-@(1|2)' ]");
}
});
});
});