-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.test.js
97 lines (85 loc) · 2.59 KB
/
index.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
const buildMain = require(".");
let lastExitCode;
beforeAll(() => {
process.chdir("..");
});
function mockConsole() {
const streamMap = {
log: "stdout",
error: "stderr",
};
const output = {};
const originals = {};
for (let [name, stream] of Object.entries(streamMap)) {
output[stream] = "";
originals[name] = console[name];
console[name] = (...args) => {
output[stream] += args.join(" ") + "\n";
};
}
return {
output,
restore: () => {
for (let [name, fn] of Object.entries(originals))
console[name] = fn;
},
};
}
async function build(...parameters) {
process.argv = ["", "", ...parameters];
process.exit = (exitCode) => {
lastExitCode = exitCode;
};
const consoleMock = mockConsole();
await buildMain();
consoleMock.restore();
return {
exitCode: lastExitCode,
stdout: consoleMock.output.stdout,
stderr: consoleMock.output.stderr,
};
}
test("Valid command", async () => {
const { exitCode, stderr } = await build("list");
expect(exitCode).toEqual(0);
expect(stderr).toEqual("");
});
test("Invalid command", async () => {
const { exitCode, stderr } = await build("foo");
expect(exitCode).not.toEqual(0);
expect(stderr).not.toEqual("");
});
// The following tests would be more robust when using stub data, but for the
// sake of progress they are currently written against the actual packages.
const parsePackageList = (stdout) =>
stdout
.split("\n")
.map((line) => {
const match = line.match(/^\s*(.*) \(/);
return match ? match[1] : null;
})
.filter((name) => !!name);
async function listPackages({ start }) {
const parameters = ["list"];
if (start) {
parameters.push("--start");
parameters.push(start);
}
const { stdout } = await build(...parameters);
return parsePackageList(stdout);
}
test("Leaf package as start", async () => {
const leaf = "feature-bundle";
const packages = await listPackages({ start: leaf });
expect(packages).toEqual([leaf]);
});
test("Start package with only direct dependants", async () => {
const start = "poly-explorer-feature";
const packages = await listPackages({ start });
expect(packages.sort()).toEqual([start, "feature-bundle"].sort());
});
test("Start package with transitive dependants", async () => {
const start = "@polypoly-eu/podjs";
const packages = await listPackages({ start });
expect(packages).toContain("feature-bundle");
});