Skip to content

Commit

Permalink
test: move the confirm mock code to its own file (#148)
Browse files Browse the repository at this point in the history
* test: move the confirm mock code to its own file

This commit moves the mocking code for the `confirm()` helper out of the main test file and into a helper file.
It also adds an equivalent helper for `prompt()`.

Apart from keeping the main test file simpler, it also will allow reuse in other test files in the future.
  • Loading branch information
petebacondarwin authored Dec 21, 2021
1 parent 3a23c5c commit 03b8851
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 33 deletions.
5 changes: 4 additions & 1 deletion packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@
"sourcemap": true
}
]
}
},
"setupFilesAfterEnv": [
"<rootDir>/src/__tests__/jest.setup.ts"
]
}
}
33 changes: 1 addition & 32 deletions packages/wrangler/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>("../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();
Expand Down
17 changes: 17 additions & 0 deletions packages/wrangler/src/__tests__/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -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."
);
});
65 changes: 65 additions & 0 deletions packages/wrangler/src/__tests__/mock-dialogs.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
);
}

0 comments on commit 03b8851

Please sign in to comment.