Skip to content

Commit

Permalink
chore: improve c3 tests (#6734)
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung authored Sep 17, 2024
1 parent e4fe35c commit 76ece75
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
13 changes: 3 additions & 10 deletions packages/create-cloudflare/e2e-tests/frameworks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,20 +660,13 @@ const verifyBuildCfTypesScript = async (
const outputFileContentPost = readFile(join(projectPath, outputFile));
const outputFileContentPostLines = outputFileContentPost.split("\n");

// the file doesn't contain the "Generated by Wrangler" comment anymore
expect(outputFileContentPostLines).not.toContain("// Generated by Wrangler");

// the file still contains the env interface
expect(outputFileContentPostLines).toContain(
`interface ${envInterfaceName} {`,
);

// the file doesn't contain the "Generated by Wrangler" comment without a timestamp anymore
expect(outputFileContentPostLines).not.toContain("// Generated by Wrangler");

// but it contains the "Generated by Wrangler" comment now with a timestamp
expect(
/\/\/ Generated by Wrangler on [a-zA-Z]*? [a-zA-Z]*? \d{2} \d{4} \d{2}:\d{2}:\d{2}/.test(
outputFileContentPost,
),
).toBeTruthy();
};

const verifyBuildScript = async (
Expand Down
29 changes: 18 additions & 11 deletions packages/create-cloudflare/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { crash } from "@cloudflare/cli";
import { processArgument } from "@cloudflare/cli/args";
import { inputPrompt } from "@cloudflare/cli/interactive";
import { mockPackageManager, mockSpinner } from "helpers/__tests__/mocks";
import { runCommand } from "helpers/command";
import { readFile } from "helpers/files";
Expand All @@ -10,7 +10,6 @@ import { createTestContext } from "./helpers";

vi.mock("helpers/command");
vi.mock("../wrangler/accounts");
vi.mock("@cloudflare/cli/args");
vi.mock("@cloudflare/cli/interactive");
vi.mock("which-pm-runs");
vi.mock("@cloudflare/cli");
Expand All @@ -33,16 +32,24 @@ const mockInsideGitRepo = (isInside = true) => {
describe("deploy helpers", async () => {
beforeEach(() => {
mockPackageManager("npm");

mockSpinner();
vi.mocked(inputPrompt).mockImplementation(async (options) => {
if (options.acceptDefault) {
return options.defaultValue;
}

throw new Error(
"If you don't want to accept the default, you must mock this function.",
);
});
});

describe("offerToDeploy", async () => {
test("user selects yes and succeeds", async () => {
const ctx = createTestContext();
ctx.template.platform = "pages";
// mock the user selecting yes when asked to deploy
vi.mocked(processArgument).mockResolvedValueOnce(true);
vi.mocked(inputPrompt).mockResolvedValueOnce(true);
// mock a successful wrangler login
vi.mocked(wranglerLogin).mockResolvedValueOnce(true);

Expand All @@ -55,7 +62,7 @@ describe("deploy helpers", async () => {
vi.mocked(readFile).mockReturnValue(`binding = "MY_QUEUE"`);

await expect(offerToDeploy(ctx)).resolves.toBe(false);
expect(processArgument).toHaveBeenCalledOnce();
expect(inputPrompt).toHaveBeenCalledOnce();
expect(ctx.args.deploy).toBe(false);
expect(wranglerLogin).not.toHaveBeenCalled();
});
Expand All @@ -72,7 +79,7 @@ describe("deploy helpers", async () => {
`);

await expect(offerToDeploy(ctx)).resolves.toBe(false);
expect(processArgument).toHaveBeenCalledOnce();
expect(inputPrompt).toHaveBeenCalledOnce();
expect(ctx.args.deploy).toBe(false);
expect(wranglerLogin).not.toHaveBeenCalled();
});
Expand All @@ -83,13 +90,13 @@ describe("deploy helpers", async () => {
experimental_assets = { directory = "./dist", binding = "ASSETS" }
`);
// mock the user selecting yes when asked to deploy
vi.mocked(processArgument).mockResolvedValueOnce(true);
vi.mocked(inputPrompt).mockResolvedValueOnce(true);
// mock a successful wrangler login
vi.mocked(wranglerLogin).mockResolvedValueOnce(true);

await expect(offerToDeploy(ctx)).resolves.toBe(true);
expect(processArgument).toHaveBeenCalledOnce();
expect(ctx.args.deploy).toBe(false);
expect(inputPrompt).toHaveBeenCalledOnce();
expect(ctx.args.deploy).toBe(true);
expect(wranglerLogin).toHaveBeenCalled();
});

Expand All @@ -99,15 +106,15 @@ describe("deploy helpers", async () => {
ctx.template.platform = "pages";

await expect(offerToDeploy(ctx)).resolves.toBe(false);
expect(processArgument).toHaveBeenCalledOnce();
expect(inputPrompt).toHaveBeenCalledOnce();
expect(ctx.args.deploy).toBe(false);
expect(wranglerLogin).not.toHaveBeenCalled();
});

test("wrangler login failure", async () => {
const ctx = createTestContext();
ctx.template.platform = "pages";
vi.mocked(processArgument).mockResolvedValueOnce(true);
vi.mocked(inputPrompt).mockResolvedValueOnce(true);
vi.mocked(wranglerLogin).mockResolvedValueOnce(false);

await expect(offerToDeploy(ctx)).resolves.toBe(false);
Expand Down

0 comments on commit 76ece75

Please sign in to comment.