Skip to content

Commit 5b2c51a

Browse files
committed
Add tests
1 parent b03b8e3 commit 5b2c51a

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

‎packages/cli/src/commands/publish/__tests__/npm-utils.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,67 @@ describe("getPackageInfo", () => {
260260

261261
expect(result).toEqual(packageData);
262262
});
263+
264+
it("uses yarn npm info when packageManager field specifies yarn berry", async () => {
265+
const cwd = await testdir({
266+
"package.json": JSON.stringify({
267+
private: true,
268+
packageManager: "[email protected]",
269+
}),
270+
});
271+
272+
mockSpawn.mockImplementation(((cmd: string, args: string[]) =>
273+
Promise.resolve(
274+
cmd === "yarn" && args?.[0] === "npm" && args?.[1] === "info"
275+
? spawnResult(
276+
JSON.stringify({
277+
name: "pkg-a",
278+
version: "1.0.0",
279+
versions: ["1.0.0"],
280+
})
281+
)
282+
: spawnResult("", 1)
283+
)) as any);
284+
285+
const result = await getPackageInfo(
286+
{ name: "pkg-a", version: "1.0.0" },
287+
cwd
288+
);
289+
290+
expect(result).toMatchObject({ name: "pkg-a" });
291+
expect(
292+
mockSpawn.mock.calls.some(
293+
([cmd, args]) =>
294+
cmd === "yarn" && args?.[0] === "npm" && args?.[1] === "info"
295+
)
296+
).toBe(true);
297+
expect(
298+
mockSpawn.mock.calls.some(
299+
([cmd, args]) => cmd === "npm" && args?.[0] === "info"
300+
)
301+
).toBe(false);
302+
});
303+
304+
it("throws when yarn classic returns unparseable non-JSON output", async () => {
305+
// isYarnClassicError's JSON.parse throws on non-JSON output, returning false
306+
// from the catch block. The output is treated as non-empty, so jsonParse
307+
// is called next and propagates the SyntaxError.
308+
const cwd = await testdir({
309+
"package.json": JSON.stringify({
310+
private: true,
311+
packageManager: "[email protected]",
312+
}),
313+
});
314+
315+
mockSpawn.mockImplementation(((cmd: string, args: string[]) =>
316+
Promise.resolve(
317+
cmd === "yarn" && args?.[0] === "info"
318+
? spawnResult("error: network timeout")
319+
: spawnResult("", 1)
320+
)) as any);
321+
322+
await expect(
323+
getPackageInfo({ name: "pkg-a", version: "1.0.0" }, cwd)
324+
).rejects.toThrow(SyntaxError);
325+
});
263326
});

‎packages/cli/src/commands/publish/__tests__/publishPackages.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,48 @@ describe("publishPackages", () => {
200200
);
201201
});
202202

203+
it("passes the otp token to npm publish when provided", async () => {
204+
const cwd = await testdir({
205+
"package.json": JSON.stringify({
206+
private: true,
207+
workspaces: ["packages/*"],
208+
}),
209+
"packages/pkg-a/package.json": JSON.stringify({
210+
name: "pkg-a",
211+
version: "1.0.0",
212+
}),
213+
});
214+
215+
mockSpawnImplementation((cmd, args) => {
216+
if (cmd === "npm" && args?.[0] === "info") {
217+
return spawnResult("");
218+
}
219+
if (cmd === "npm" && args?.[0] === "publish") {
220+
return spawnResult(JSON.stringify({ id: "[email protected]" }));
221+
}
222+
return spawnResult("", 1);
223+
});
224+
225+
const result = await publishPackages({
226+
packages: (await getPackages(cwd)).packages,
227+
access: "public",
228+
preState: undefined,
229+
otp: "123456",
230+
cwd,
231+
});
232+
233+
expect(result).toEqual([
234+
{ name: "pkg-a", newVersion: "1.0.0", result: "published" },
235+
]);
236+
237+
const publishCall = mockSpawn.mock.calls.find(
238+
([cmd, args]) => cmd === "npm" && args?.[0] === "publish"
239+
);
240+
expect(publishCall?.[1]).toEqual(
241+
expect.arrayContaining(["--otp", "123456"])
242+
);
243+
});
244+
203245
it("publishes with specified tag on GitHub Packages when new version not yet published", async () => {
204246
// GitHub Packages does not auto-assign latest on first publish. When both
205247
// the bare query and the exact-version fallback return empty, there is no

0 commit comments

Comments
 (0)