|
| 1 | +import { FatalError } from "@cloudflare/workers-utils"; |
| 2 | +import { vi } from "vitest"; |
| 3 | +import * as details from "../autoconfig/get-details"; |
| 4 | +import * as run from "../autoconfig/run"; |
| 5 | +import * as format from "../deployment-bundle/guess-worker-format"; |
| 6 | +import { clearOutputFilePath } from "../output"; |
| 7 | +import { mockAccountId, mockApiToken } from "./helpers/mock-account-id"; |
| 8 | +import { clearDialogs } from "./helpers/mock-dialogs"; |
| 9 | +import { useMockIsTTY } from "./helpers/mock-istty"; |
| 10 | +import { runInTempDir } from "./helpers/run-in-tmp"; |
| 11 | +import { runWrangler } from "./helpers/run-wrangler"; |
| 12 | +import { writeWorkerSource } from "./helpers/write-worker-source"; |
| 13 | +import { writeWranglerConfig } from "./helpers/write-wrangler-config"; |
| 14 | + |
| 15 | +vi.mock("../deploy/deploy", async (importOriginal) => ({ |
| 16 | + ...(await importOriginal()), |
| 17 | + default: () => { |
| 18 | + // In unit tests of autoconfig we only care about the configuration aspect, so bail before any actual deployment happens |
| 19 | + throw new FatalError("Bailing early in tests"); |
| 20 | + }, |
| 21 | +})); |
| 22 | + |
| 23 | +async function runDeploy(withArgs: string = "") { |
| 24 | + // Expect "Bailing early in tests" to be thrown |
| 25 | + await expect(runWrangler(`deploy ${withArgs}`)).rejects.toThrowError(); |
| 26 | +} |
| 27 | + |
| 28 | +// We don't care about module/service worker detection in the autoconfig tests, |
| 29 | +// and mocking it out speeds up the tests by removing an esbuild invocation |
| 30 | +vi.spyOn(format, "guessWorkerFormat").mockImplementation(() => |
| 31 | + Promise.resolve({ |
| 32 | + format: "modules", |
| 33 | + exports: [], |
| 34 | + }) |
| 35 | +); |
| 36 | + |
| 37 | +describe("autoconfig (deploy)", () => { |
| 38 | + mockAccountId(); |
| 39 | + mockApiToken(); |
| 40 | + runInTempDir(); |
| 41 | + const { setIsTTY } = useMockIsTTY(); |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + setIsTTY(true); |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + vi.unstubAllGlobals(); |
| 49 | + clearDialogs(); |
| 50 | + clearOutputFilePath(); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should not check for autoconfig without flag", async () => { |
| 54 | + writeWorkerSource(); |
| 55 | + writeWranglerConfig({ main: "index.js" }); |
| 56 | + const getDetailsSpy = vi.spyOn(details, "getDetailsForAutoConfig"); |
| 57 | + await runDeploy(); |
| 58 | + |
| 59 | + expect(getDetailsSpy).not.toHaveBeenCalled(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should check for autoconfig with flag", async () => { |
| 63 | + const getDetailsSpy = vi.spyOn(details, "getDetailsForAutoConfig"); |
| 64 | + |
| 65 | + await runDeploy("--x-autoconfig"); |
| 66 | + |
| 67 | + expect(getDetailsSpy).toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should run autoconfig if project is not configured", async () => { |
| 71 | + const getDetailsSpy = vi |
| 72 | + .spyOn(details, "getDetailsForAutoConfig") |
| 73 | + .mockImplementationOnce(() => Promise.resolve({ configured: false })); |
| 74 | + const runSpy = vi.spyOn(run, "runAutoConfig"); |
| 75 | + |
| 76 | + await runDeploy("--x-autoconfig"); |
| 77 | + |
| 78 | + expect(getDetailsSpy).toHaveBeenCalled(); |
| 79 | + expect(runSpy).toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should not run autoconfig if project is already configured", async () => { |
| 83 | + const getDetailsSpy = vi |
| 84 | + .spyOn(details, "getDetailsForAutoConfig") |
| 85 | + .mockImplementationOnce(() => Promise.resolve({ configured: true })); |
| 86 | + const runSpy = vi.spyOn(run, "runAutoConfig"); |
| 87 | + |
| 88 | + await runDeploy("--x-autoconfig"); |
| 89 | + |
| 90 | + expect(getDetailsSpy).toHaveBeenCalled(); |
| 91 | + expect(runSpy).not.toHaveBeenCalled(); |
| 92 | + }); |
| 93 | +}); |
0 commit comments