/
add.ts
231 lines (201 loc) 路 6.84 KB
/
add.ts
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
import path from "path";
import fixtures from "fixturez";
import stripAnsi from "strip-ansi";
import * as git from "@changesets/git";
import { defaultConfig } from "@changesets/config";
import { silenceLogsInBlock } from "@changesets/test-utils";
import writeChangeset from "@changesets/write";
import {
askCheckboxPlus,
askConfirm,
askQuestionWithEditor,
askQuestion,
askList
} from "../../../utils/cli-utilities";
import addChangeset from "..";
const f = fixtures(__dirname);
jest.mock("../../../utils/cli-utilities");
jest.mock("@changesets/git");
jest.mock("@changesets/write");
// @ts-ignore
writeChangeset.mockImplementation(() => Promise.resolve("abcdefg"));
// @ts-ignore
git.commit.mockImplementation(() => Promise.resolve(true));
// @ts-ignore
git.getChangedPackagesSinceRef.mockImplementation(({ ref }) => {
expect(ref).toBe("master");
return [];
});
// @ts-ignore
const mockUserResponses = mockResponses => {
const summary = mockResponses.summary || "summary message mock";
let majorReleases: Array<string> = [];
let minorReleases: Array<string> = [];
Object.entries(mockResponses.releases).forEach(([pkgName, type]) => {
if (type === "major") {
majorReleases.push(pkgName);
} else if (type === "minor") {
minorReleases.push(pkgName);
}
});
let callCount = 0;
let returnValues = [
Object.keys(mockResponses.releases),
majorReleases,
minorReleases
];
// @ts-ignore
askCheckboxPlus.mockImplementation(() => {
if (callCount === returnValues.length) {
throw new Error(`There was an unexpected call to askCheckboxPlus`);
}
return returnValues[callCount++];
});
let confirmAnswers = {
"Is this your desired changeset?": true
};
if (mockResponses.consoleSummaries && mockResponses.editorSummaries) {
let i = 0;
let j = 0;
// @ts-ignore
askQuestion.mockImplementation(() => mockResponses.consoleSummaries[i++]);
// @ts-ignore
askQuestionWithEditor.mockImplementation(
() => mockResponses.editorSummaries[j++]
);
} else {
// @ts-ignore
askQuestion.mockReturnValueOnce(summary);
}
// @ts-ignore
askConfirm.mockImplementation(question => {
question = stripAnsi(question);
// @ts-ignore
if (confirmAnswers[question]) {
// @ts-ignore
return confirmAnswers[question];
}
throw new Error(`An answer could not be found for ${question}`);
});
};
describe("Changesets", () => {
silenceLogsInBlock();
it("should generate changeset to patch a single package", async () => {
const cwd = await f.copy("simple-project");
mockUserResponses({ releases: { "pkg-a": "patch" } });
await addChangeset(cwd, { empty: false }, defaultConfig);
// @ts-ignore
const call = writeChangeset.mock.calls[0][0];
expect(call).toEqual(
expect.objectContaining({
summary: "summary message mock",
releases: [{ name: "pkg-a", type: "patch" }]
})
);
});
it.each`
consoleSummaries | editorSummaries | expectedSummary
${["summary on step 1"]} | ${[]} | ${"summary on step 1"}
${[""]} | ${["summary in external editor"]} | ${"summary in external editor"}
${["", "summary after editor cancelled"]} | ${[""]} | ${"summary after editor cancelled"}
${["", "summary after error"]} | ${1 /* mock implementation will throw */} | ${"summary after error"}
`(
"should read summary",
// @ts-ignore
async ({ consoleSummaries, editorSummaries, expectedSummary }) => {
const cwd = await f.copy("simple-project");
mockUserResponses({
releases: { "pkg-a": "patch" },
consoleSummaries,
editorSummaries
});
await addChangeset(cwd, { empty: false }, defaultConfig);
// @ts-ignore
const call = writeChangeset.mock.calls[0][0];
expect(call).toEqual(
expect.objectContaining({
summary: expectedSummary,
releases: [{ name: "pkg-a", type: "patch" }]
})
);
}
);
it("should generate a changeset in a single package repo", async () => {
const cwd = await f.copy("single-package");
const summary = "summary message mock";
// @ts-ignore
askList.mockReturnValueOnce(Promise.resolve("minor"));
let confirmAnswers = {
"Is this your desired changeset?": true
};
// @ts-ignore
askQuestion.mockReturnValueOnce("");
// @ts-ignore
askQuestionWithEditor.mockReturnValueOnce(summary);
// @ts-ignore
askConfirm.mockImplementation(question => {
question = stripAnsi(question);
// @ts-ignore
if (confirmAnswers[question]) {
// @ts-ignore
return confirmAnswers[question];
}
throw new Error(`An answer could not be found for ${question}`);
});
await addChangeset(cwd, { empty: false }, defaultConfig);
// @ts-ignore
const call = writeChangeset.mock.calls[0][0];
expect(call).toEqual(
expect.objectContaining({
summary: "summary message mock",
releases: [{ name: "single-package", type: "minor" }]
})
);
});
it("should commit when the commit flag is passed in", async () => {
const cwd = await f.copy("simple-project-custom-config");
mockUserResponses({ releases: { "pkg-a": "patch" } });
await addChangeset(
cwd,
{ empty: false },
{
...defaultConfig,
commit: [path.resolve(__dirname, "..", "..", "..", "commit"), null]
}
);
expect(git.add).toHaveBeenCalledTimes(1);
expect(git.commit).toHaveBeenCalledTimes(1);
});
it("should create empty changeset when empty flag is passed in", async () => {
const cwd = await f.copy("simple-project");
await addChangeset(cwd, { empty: true }, defaultConfig);
// @ts-ignore
const call = writeChangeset.mock.calls[0][0];
expect(call).toEqual(
expect.objectContaining({
releases: [],
summary: ""
})
);
});
it("should not include ignored packages in the prompt", async () => {
const cwd = await f.copy("internal-dependencies");
mockUserResponses({ releases: { "pkg-a": "patch" } });
await addChangeset(
cwd,
{ empty: false },
{ ...defaultConfig, ignore: ["pkg-b"] }
);
// @ts-ignore
const { choices } = askCheckboxPlus.mock.calls[0][1][0];
expect(choices).toEqual(["pkg-a", "pkg-c"]);
});
it("should not include private packages without a version in the prompt", async () => {
const cwd = await f.copy("private-package-without-version-field");
mockUserResponses({ releases: { "pkg-a": "patch" } });
await addChangeset(cwd, { empty: false }, defaultConfig);
// @ts-ignore
const { choices } = askCheckboxPlus.mock.calls[0][1][0];
expect(choices).toEqual(["pkg-a", "pkg-c"]);
});
});