Skip to content

Commit

Permalink
check if throw instead of crash
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Sep 6, 2024
1 parent a59d911 commit 2b6c442
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
13 changes: 6 additions & 7 deletions packages/create-cloudflare/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { crash } from "@cloudflare/cli";
import { mockPackageManager, mockSpinner } from "helpers/__tests__/mocks";
import { processArgument } from "helpers/args";
import { runCommand } from "helpers/command";
Expand All @@ -13,7 +12,6 @@ vi.mock("../wrangler/accounts");
vi.mock("helpers/args");
vi.mock("@cloudflare/cli/interactive");
vi.mock("which-pm-runs");
vi.mock("@cloudflare/cli");
vi.mock("helpers/files");

const mockInsideGitRepo = (isInside = true) => {
Expand Down Expand Up @@ -95,7 +93,6 @@ describe("deploy helpers", async () => {
vi.mocked(runCommand).mockResolvedValueOnce(deployedUrl);

await runDeploy(ctx);
expect(crash).not.toHaveBeenCalled();
expect(runCommand).toHaveBeenCalledWith(
["npm", "run", "deploy", "--", "--commit-message", `"${commitMsg}"`],
expect.any(Object),
Expand All @@ -106,8 +103,9 @@ describe("deploy helpers", async () => {
test("no account in ctx", async () => {
const ctx = createTestContext();
ctx.account = undefined;
await runDeploy(ctx);
expect(crash).toHaveBeenCalledWith("Failed to read Cloudflare account.");
await expect(() => runDeploy(ctx)).rejects.toThrow(
"Failed to read Cloudflare account.",
);
});

test("Failed deployment", async () => {
Expand All @@ -118,8 +116,9 @@ describe("deploy helpers", async () => {
mockInsideGitRepo(false);
vi.mocked(runCommand).mockResolvedValueOnce("");

await runDeploy(ctx);
expect(crash).toHaveBeenCalledWith("Failed to find deployment url.");
await expect(() => runDeploy(ctx)).rejects.toThrow(
"Failed to find deployment url.",
);
});
});
});
29 changes: 11 additions & 18 deletions packages/create-cloudflare/src/__tests__/templates.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { existsSync, statSync } from "fs";
import { crash } from "@cloudflare/cli";
import { spinner } from "@cloudflare/cli/interactive";
import degit from "degit";
import { mockSpinner } from "helpers/__tests__/mocks";
Expand All @@ -21,7 +20,6 @@ import type { C3Args, C3Context } from "types";
vi.mock("degit");
vi.mock("fs");
vi.mock("helpers/files");
vi.mock("@cloudflare/cli");
vi.mock("@cloudflare/cli/interactive");

beforeEach(() => {
Expand Down Expand Up @@ -305,22 +303,17 @@ describe("deriveCorrelatedArgs", () => {
});

test("should crash if both the lang and ts arguments are specified", () => {
let args: Partial<C3Args> = {
lang: "ts",
};

deriveCorrelatedArgs(args);

expect(args.lang).toBe("ts");
expect(crash).not.toBeCalled();

args = {
ts: true,
lang: "ts",
};
deriveCorrelatedArgs(args);

expect(crash).toBeCalledWith(
expect(() =>
deriveCorrelatedArgs({
lang: "ts",
}),
).not.toThrow();
expect(() =>
deriveCorrelatedArgs({
ts: true,
lang: "ts",
}),
).toThrow(
"The `--ts` argument cannot be specified in conjunction with the `--lang` argument",
);
});
Expand Down

0 comments on commit 2b6c442

Please sign in to comment.