-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
version-lifecycle-scripts.test.js
98 lines (78 loc) · 2.7 KB
/
version-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
"use strict";
// local modules _must_ be explicitly mocked
jest.mock("../lib/git-push");
jest.mock("../lib/is-anything-committed");
jest.mock("../lib/is-behind-upstream");
jest.mock("../lib/remote-branch-exists");
// mocked modules
const runLifecycle = require("@lerna/run-lifecycle");
const loadJsonFile = require("load-json-file");
// helpers
const initFixture = require("@lerna-test/init-fixture")(__dirname);
// test command
const lernaVersion = 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 version lifecycle scripts for root and packages", async () => {
const cwd = await initFixture("lifecycle");
await lernaVersion(cwd)();
expect(runLifecycle).toHaveBeenCalledTimes(6);
["preversion", "version", "postversion"].forEach(script => {
// "lifecycle" is the root manifest name
expect(runLifecycle).toHaveBeenCalledWith(
expect.objectContaining({ name: "lifecycle" }),
script,
expect.any(Object)
);
expect(runLifecycle).toHaveBeenCalledWith(
expect.objectContaining({ name: "package-1" }),
script,
expect.any(Object)
);
});
// package-2 lacks version lifecycle scripts
expect(runLifecycle).not.toHaveBeenCalledWith(
expect.objectContaining({ name: "package-2" }),
expect.any(String)
);
expect(runLifecycle.getOrderedCalls()).toEqual([
["lifecycle", "preversion"],
["package-1", "preversion"],
["package-1", "version"],
["lifecycle", "version"],
["package-1", "postversion"],
["lifecycle", "postversion"],
]);
expect(loadJsonFile.registry).toMatchInlineSnapshot(`
Map {
"/packages/package-1" => 2,
"/packages/package-2" => 2,
}
`);
});
it("does not execute recursive root scripts", async () => {
const cwd = await initFixture("lifecycle");
process.env.npm_lifecycle_event = "version";
await lernaVersion(cwd)();
expect(runLifecycle.getOrderedCalls()).toEqual([
["package-1", "preversion"],
["package-1", "version"],
["package-1", "postversion"],
]);
});
it("does not duplicate rooted leaf scripts", async () => {
const cwd = await initFixture("lifecycle-rooted-leaf");
await lernaVersion(cwd)();
expect(runLifecycle.getOrderedCalls()).toEqual([
["package-1", "preversion"],
["package-1", "version"],
["lifecycle-rooted-leaf", "preversion"],
["lifecycle-rooted-leaf", "version"],
["lifecycle-rooted-leaf", "postversion"],
["package-1", "postversion"],
]);
});
});