-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
publish-lifecycle-scripts.test.js
139 lines (116 loc) · 4.27 KB
/
publish-lifecycle-scripts.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
"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");
// 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");
// mocked modules
const packDirectory = require("@lerna/pack-directory");
const runLifecycle = require("@lerna/run-lifecycle");
const loadJsonFile = require("load-json-file");
// helpers
const initFixture = require("@lerna-test/init-fixture")(__dirname);
const path = require("path");
// test command
const lernaPublish = require("@lerna-test/command-runner")(require("../command"));
describe("lifecycle scripts", () => {
const npmLifecycleEvent = process.env.npm_lifecycle_event;
afterEach(() => {
process.env.npm_lifecycle_event = npmLifecycleEvent;
});
it("calls publish lifecycle scripts for root and packages", async () => {
const cwd = await initFixture("lifecycle");
await lernaPublish(cwd)();
["prepare", "prepublishOnly", "prepack", "postpack", "postpublish"].forEach(script => {
// "lifecycle" is the root manifest name
expect(runLifecycle).toHaveBeenCalledWith(
expect.objectContaining({ name: "lifecycle" }),
script,
expect.any(Object)
);
});
// package-2 only has prepublish lifecycle
expect(packDirectory).toHaveBeenCalledWith(
expect.objectContaining({ name: "package-2" }),
path.join(cwd, "packages/package-2"),
expect.objectContaining({
"ignore-prepublish": false,
"ignore-scripts": false,
})
);
expect(runLifecycle.getOrderedCalls()).toEqual([
// TODO: separate from VersionCommand details
["lifecycle", "preversion"],
["package-1", "preversion"],
["package-1", "version"],
["lifecycle", "version"],
["package-1", "postversion"],
["lifecycle", "postversion"],
// publish-specific
["lifecycle", "prepublish"],
["lifecycle", "prepare"],
["lifecycle", "prepublishOnly"],
["lifecycle", "prepack"],
["lifecycle", "postpack"],
["lifecycle", "postpublish"],
]);
expect(loadJsonFile.registry).toMatchInlineSnapshot(`
Map {
"/packages/package-1" => 4,
"/packages/package-2" => 4,
}
`);
});
it("does not execute recursive root scripts", async () => {
const cwd = await initFixture("lifecycle");
process.env.npm_lifecycle_event = "prepublish";
await lernaPublish(cwd)();
expect(runLifecycle.getOrderedCalls()).toEqual([
// TODO: separate from VersionCommand details
["lifecycle", "preversion"],
["package-1", "preversion"],
["package-1", "version"],
["lifecycle", "version"],
["package-1", "postversion"],
["lifecycle", "postversion"],
// publish-specific
["lifecycle", "prepare"],
["lifecycle", "prepublishOnly"],
["lifecycle", "prepack"],
["lifecycle", "postpack"],
]);
});
it("does not duplicate rooted leaf scripts", async () => {
const cwd = await initFixture("lifecycle-rooted-leaf");
await lernaPublish(cwd)();
expect(runLifecycle.getOrderedCalls()).toEqual([
// TODO: separate from VersionCommand details
["package-1", "preversion"],
["package-1", "version"],
["lifecycle-rooted-leaf", "preversion"],
["lifecycle-rooted-leaf", "version"],
["lifecycle-rooted-leaf", "postversion"],
["package-1", "postversion"],
// NO publish-specific root lifecycles should be duplicated
// (they are all run by pack-directory and npm-publish)
]);
});
it("respects --ignore-prepublish", async () => {
const cwd = await initFixture("lifecycle");
await lernaPublish(cwd)("--ignore-prepublish");
expect(packDirectory).toHaveBeenCalledWith(
expect.objectContaining({ name: "package-2" }),
path.join(cwd, "packages/package-2"),
expect.objectContaining({
"ignore-prepublish": true,
})
);
// runLifecycle() is _called_ with "prepublish" for root,
// but it does not actually execute, and is tested elsewhere
});
});