diff --git a/packages/wrangler/package.json b/packages/wrangler/package.json index 3a945610f1ea..1835021820fb 100644 --- a/packages/wrangler/package.json +++ b/packages/wrangler/package.json @@ -109,6 +109,9 @@ "sourcemap": true } ] - } + }, + "setupFilesAfterEnv": [ + "/src/__tests__/jest.setup.ts" + ] } } diff --git a/packages/wrangler/src/__tests__/index.test.ts b/packages/wrangler/src/__tests__/index.test.ts index 0ac63d7edcae..8461ed433a90 100644 --- a/packages/wrangler/src/__tests__/index.test.ts +++ b/packages/wrangler/src/__tests__/index.test.ts @@ -5,41 +5,10 @@ import * as path from "node:path"; import * as TOML from "@iarna/toml"; import { main } from "../index"; import { setMock, unsetAllMocks } from "./mock-cfetch"; -import { confirm } from "../dialogs"; +import { mockConfirm } from "./mock-dialogs"; jest.mock("../cfetch", () => jest.requireActual("./mock-cfetch")); -jest.mock("../dialogs", () => { - return { - ...jest.requireActual("../dialogs"), - confirm: jest - .fn() - .mockName("confirmMock") - .mockImplementation(() => { - // By default (if not configured by mockConfirm()) calls to `confirm()` should throw. - throw new Error("Unexpected call to `confirm()`."); - }), - }; -}); - -/** - * Mock the implementation of `confirm()` that will respond with configured results - * for configured confirmation text messages. - * - * If there is a call to `confirm()` that does not match any of the expectations - * then an error is thrown. - */ -function mockConfirm(...expectations: { text: string; result: boolean }[]) { - (confirm as jest.Mock).mockImplementation((text: string) => { - for (const { text: expectedText, result } of expectations) { - if (text === expectedText) { - return result; - } - } - throw new Error(`Unexpected confirmation message: ${text}`); - }); -} - async function w(cmd?: string) { const logSpy = jest.spyOn(console, "log").mockImplementation(); const errorSpy = jest.spyOn(console, "error").mockImplementation(); diff --git a/packages/wrangler/src/__tests__/jest.setup.ts b/packages/wrangler/src/__tests__/jest.setup.ts new file mode 100644 index 000000000000..59b22afddede --- /dev/null +++ b/packages/wrangler/src/__tests__/jest.setup.ts @@ -0,0 +1,17 @@ +import { confirm, prompt } from "../dialogs"; + +jest.mock("../dialogs"); + +// By default (if not configured by mockConfirm()) calls to `confirm()` should throw. +(confirm as jest.Mock).mockImplementation(() => { + throw new Error( + "Unexpected call to `confirm()`. You should use `mockConfirm()` to mock calls to `confirm()` with expectations. Search the codebase for `mockConfirm` to learn more." + ); +}); + +// By default (if not configured by mockPrompt()) calls to `prompt()` should throw. +(prompt as jest.Mock).mockImplementation(() => { + throw new Error( + "Unexpected call to `prompt()`. You should use `mockPrompt()` to mock calls to `prompt()` with expectations. Search the codebase for `mockPrompt` to learn more." + ); +}); diff --git a/packages/wrangler/src/__tests__/mock-dialogs.ts b/packages/wrangler/src/__tests__/mock-dialogs.ts new file mode 100644 index 000000000000..b035675be4fc --- /dev/null +++ b/packages/wrangler/src/__tests__/mock-dialogs.ts @@ -0,0 +1,65 @@ +import { confirm, prompt } from "../dialogs"; + +/** + * The expected values for a confirmation request. + */ +export interface ConfirmExpectation { + /** The text expected to be seen in the confirmation dialog. */ + text: string; + /** The mock response send back from the confirmation dialog. */ + result: boolean; +} + +/** + * Mock the implementation of `confirm()` that will respond with configured results + * for configured confirmation text messages. + * + * If there is a call to `confirm()` that does not match any of the expectations + * then an error is thrown. + */ +export function mockConfirm(...expectations: ConfirmExpectation[]) { + (confirm as jest.Mock).mockImplementation((text: string) => { + for (const { text: expectedText, result } of expectations) { + if (text === expectedText) { + return Promise.resolve(result); + } + } + throw new Error(`Unexpected confirmation message: ${text}`); + }); +} + +/** + * The expected values for a prompt request. + */ +export interface PromptExpectation { + /** The text expected to be seen in the prompt dialog. */ + text: string; + /** The type of the prompt. */ + type: "text" | "password"; + /** The mock response send back from the prompt dialog. */ + result: string; +} + +/** + * Mock the implementation of `prompt()` that will respond with configured results + * for configured prompt text messages. + * + * If there is a call to `prompt()` that does not match any of the expectations + * then an error is thrown. + */ +export function mockPrompt(...expectations: PromptExpectation[]) { + (prompt as jest.Mock).mockImplementation( + (text: string, type: "text" | "password") => { + for (const { + text: expectedText, + type: expectedType, + result, + } of expectations) { + if (text === expectedText && type == expectedType) { + return Promise.resolve(result); + } + } + throw new Error(`Unexpected confirmation message: ${text}`); + } + ); +}