-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
publish-from-package.test.js
134 lines (101 loc) · 4.68 KB
/
publish-from-package.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
"use strict";
// local modules _must_ be explicitly mocked
jest.mock("../lib/get-packages-without-license");
jest.mock("../lib/verify-npm-package-access");
jest.mock("../lib/get-npm-username");
jest.mock("../lib/get-two-factor-auth-required");
jest.mock("../lib/get-unpublished-packages");
// FIXME: better mock for version command
jest.mock("../../version/lib/git-push");
jest.mock("../../version/lib/is-anything-committed");
jest.mock("../../version/lib/is-behind-upstream");
jest.mock("../../version/lib/remote-branch-exists");
const fs = require("fs-extra");
const path = require("path");
// mocked or stubbed modules
const writePkg = require("write-pkg");
const { npmPublish } = require("@lerna/npm-publish");
const { promptConfirmation } = require("@lerna/prompt");
const { output } = require("@lerna/output");
const { throwIfUncommitted } = require("@lerna/check-working-tree");
const { getUnpublishedPackages } = require("../lib/get-unpublished-packages");
// helpers
const { loggingOutput } = require("@lerna-test/helpers/logging-output");
const initFixture = require("@lerna-test/helpers").initFixtureFactory(__dirname);
// file under test
const lernaPublish = require("@lerna-test/helpers").commandRunner(require("../command"));
describe("publish from-package", () => {
it("publishes unpublished packages", async () => {
const cwd = await initFixture("normal");
getUnpublishedPackages.mockImplementationOnce((packageGraph) => {
const pkgs = packageGraph.rawPackageList.slice(1, 3);
return pkgs.map((pkg) => packageGraph.get(pkg.name));
});
await lernaPublish(cwd)("from-package");
expect(promptConfirmation).toHaveBeenLastCalledWith("Are you sure you want to publish these packages?");
expect(output.logged()).toMatch("Found 2 packages to publish:");
expect(npmPublish.order()).toEqual(["package-2", "package-3"]);
});
it("publishes unpublished independent packages", async () => {
const cwd = await initFixture("independent");
getUnpublishedPackages.mockImplementationOnce((packageGraph) => Array.from(packageGraph.values()));
await lernaPublish(cwd)("from-package");
expect(npmPublish.order()).toEqual([
"package-1",
"package-4",
"package-2",
"package-3",
// package-5 is private
]);
});
it("publishes unpublished independent packages, lexically sorted when --no-sort is present", async () => {
const cwd = await initFixture("independent");
getUnpublishedPackages.mockImplementationOnce((packageGraph) => Array.from(packageGraph.values()));
await lernaPublish(cwd)("from-package", "--no-sort");
expect(npmPublish.order()).toEqual([
"package-1",
"package-2",
"package-3",
"package-4",
// package-5 is private
]);
});
it("exits early when all packages are published", async () => {
const cwd = await initFixture("normal");
await lernaPublish(cwd)("from-package");
expect(npmPublish).not.toHaveBeenCalled();
const logMessages = loggingOutput("notice");
expect(logMessages).toContain("No unpublished release found");
});
it("throws an error when uncommitted changes are present", async () => {
throwIfUncommitted.mockImplementationOnce(() => {
throw new Error("uncommitted");
});
const cwd = await initFixture("normal");
const command = lernaPublish(cwd)("from-package");
await expect(command).rejects.toThrow("uncommitted");
// notably different than the actual message, but good enough here
});
it("does not require a git repo", async () => {
getUnpublishedPackages.mockImplementationOnce((packageGraph) => [packageGraph.get("package-1")]);
const cwd = await initFixture("independent");
// nuke the git repo first
await fs.remove(path.join(cwd, ".git"));
await lernaPublish(cwd)("from-package");
expect(npmPublish).toHaveBeenCalled();
expect(writePkg.updatedManifest("package-1")).not.toHaveProperty("gitHead");
const logMessages = loggingOutput("notice");
expect(logMessages).toContain("Unable to verify working tree, proceed at your own risk");
expect(logMessages).toContain(
"Unable to set temporary gitHead property, it will be missing from registry metadata"
);
expect(logMessages).toContain("Unable to reset working tree changes, this probably isn't a git repo.");
});
it("accepts --git-head override", async () => {
getUnpublishedPackages.mockImplementationOnce((packageGraph) => [packageGraph.get("package-1")]);
const cwd = await initFixture("independent");
await lernaPublish(cwd)("from-package", "--git-head", "deadbeef");
expect(npmPublish).toHaveBeenCalled();
expect(writePkg.updatedManifest("package-1").gitHead).toBe("deadbeef");
});
});