|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import { askConfirmation } from "../../utils/ask-confirmation.js"; |
| 4 | +import { createOpenNextConfigFile, findOpenNextConfig } from "../../utils/create-open-next-config.js"; |
| 5 | +import { compileConfig } from "./utils.js"; |
| 6 | + |
| 7 | +const { mockExistsSync } = vi.hoisted(() => ({ |
| 8 | + mockExistsSync: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +// Mock node:fs — only override existsSync |
| 12 | +vi.mock("node:fs", async (importOriginal) => { |
| 13 | + const mod = await importOriginal<typeof import("node:fs")>(); |
| 14 | + return { ...mod, existsSync: mockExistsSync }; |
| 15 | +}); |
| 16 | + |
| 17 | +// Mock logger |
| 18 | +vi.mock("@opennextjs/aws/logger.js", () => ({ |
| 19 | + default: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn(), setLevel: vi.fn() }, |
| 20 | +})); |
| 21 | + |
| 22 | +// Mock compileOpenNextConfig |
| 23 | +const mockCompileOpenNextConfig = vi.fn(async () => ({ |
| 24 | + config: { default: {} }, |
| 25 | + buildDir: "/build", |
| 26 | +})); |
| 27 | +vi.mock("@opennextjs/aws/build/compileConfig.js", () => ({ |
| 28 | + compileOpenNextConfig: (...args: unknown[]) => mockCompileOpenNextConfig(...args), |
| 29 | +})); |
| 30 | + |
| 31 | +// Mock ensureCloudflareConfig |
| 32 | +vi.mock("../../build/utils/ensure-cf-config.js", () => ({ |
| 33 | + ensureCloudflareConfig: vi.fn(), |
| 34 | +})); |
| 35 | + |
| 36 | +// Mock askConfirmation |
| 37 | +vi.mock("../../utils/ask-confirmation.js", () => ({ |
| 38 | + askConfirmation: vi.fn(), |
| 39 | +})); |
| 40 | + |
| 41 | +// Mock create-config-files (unused import in utils.ts but required for module resolution) |
| 42 | +vi.mock("../../utils/create-config-files.js", () => ({ |
| 43 | + createOpenNextConfigIfNotExistent: vi.fn(), |
| 44 | +})); |
| 45 | + |
| 46 | +// Mock create-open-next-config |
| 47 | +vi.mock("../../utils/create-open-next-config.js", () => ({ |
| 48 | + findOpenNextConfig: vi.fn(), |
| 49 | + createOpenNextConfigFile: vi.fn(() => "/test/open-next.config.ts"), |
| 50 | +})); |
| 51 | + |
| 52 | +// Mock wrangler |
| 53 | +vi.mock("wrangler", () => ({ |
| 54 | + unstable_readConfig: vi.fn(), |
| 55 | +})); |
| 56 | + |
| 57 | +// Mock build utils |
| 58 | +vi.mock("@opennextjs/aws/build/utils.js", () => ({ |
| 59 | + printHeader: vi.fn(), |
| 60 | + showWarningOnWindows: vi.fn(), |
| 61 | +})); |
| 62 | + |
| 63 | +// Mock build helper |
| 64 | +vi.mock("@opennextjs/aws/build/helper.js", () => ({ |
| 65 | + normalizeOptions: vi.fn(() => ({})), |
| 66 | +})); |
| 67 | + |
| 68 | +describe("compileConfig", () => { |
| 69 | + afterEach(() => { |
| 70 | + vi.restoreAllMocks(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should compile config when configPath is provided and file exists", async () => { |
| 74 | + mockExistsSync.mockReturnValue(true); |
| 75 | + |
| 76 | + const result = await compileConfig("/app/open-next.config.ts"); |
| 77 | + |
| 78 | + expect(mockCompileOpenNextConfig).toHaveBeenCalledWith("/app/open-next.config.ts", { compileEdge: true }); |
| 79 | + expect(result).toEqual({ config: { default: {} }, buildDir: "/build" }); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should throw when configPath is provided but file does not exist", async () => { |
| 83 | + mockExistsSync.mockReturnValue(false); |
| 84 | + |
| 85 | + await expect(compileConfig("/app/missing-config.ts")).rejects.toThrowErrorMatchingInlineSnapshot( |
| 86 | + `[Error: Custom config file not found at /app/missing-config.ts]` |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should compile config when no configPath is provided but one is found", async () => { |
| 91 | + vi.mocked(findOpenNextConfig).mockReturnValue("/app/open-next.config.ts"); |
| 92 | + |
| 93 | + const result = await compileConfig(undefined); |
| 94 | + |
| 95 | + expect(findOpenNextConfig).toHaveBeenCalledOnce(); |
| 96 | + expect(mockCompileOpenNextConfig).toHaveBeenCalledWith("/app/open-next.config.ts", { compileEdge: true }); |
| 97 | + expect(result).toEqual({ config: { default: {} }, buildDir: "/build" }); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should create config when no configPath found and user confirms", async () => { |
| 101 | + vi.mocked(findOpenNextConfig).mockReturnValue(undefined); |
| 102 | + vi.mocked(askConfirmation).mockResolvedValue(true); |
| 103 | + |
| 104 | + const result = await compileConfig(undefined); |
| 105 | + |
| 106 | + expect(askConfirmation).toHaveBeenCalledOnce(); |
| 107 | + expect(vi.mocked(askConfirmation).mock.calls[0]).toMatchInlineSnapshot(` |
| 108 | + [ |
| 109 | + "Missing required \`open-next.config.ts\` file, do you want to create one?", |
| 110 | + ] |
| 111 | + `); |
| 112 | + expect(createOpenNextConfigFile).toHaveBeenCalledOnce(); |
| 113 | + expect(mockCompileOpenNextConfig).toHaveBeenCalledWith("/test/open-next.config.ts", { |
| 114 | + compileEdge: true, |
| 115 | + }); |
| 116 | + expect(result).toEqual({ config: { default: {} }, buildDir: "/build" }); |
| 117 | + }); |
| 118 | + |
| 119 | + it("should throw when no configPath found and user declines", async () => { |
| 120 | + vi.mocked(findOpenNextConfig).mockReturnValue(undefined); |
| 121 | + vi.mocked(askConfirmation).mockResolvedValue(false); |
| 122 | + |
| 123 | + await expect(compileConfig(undefined)).rejects.toThrowErrorMatchingInlineSnapshot( |
| 124 | + `[Error: The \`open-next.config.ts\` file is required, aborting!]` |
| 125 | + ); |
| 126 | + |
| 127 | + expect(askConfirmation).toHaveBeenCalledOnce(); |
| 128 | + expect(createOpenNextConfigFile).not.toHaveBeenCalled(); |
| 129 | + }); |
| 130 | +}); |
0 commit comments