Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: move the confirm mock code to its own file #148

Merged

Conversation

petebacondarwin
Copy link
Contributor

This commit moves the mocking code for the confirm() helper out of the main test file and into a helper file.

Apart from keeping the main test file simpler, it also will allow reuse in other test files in the future.

NOTE that the following line must still appear in the test file to ensure the module gets mocked before it is first used.

jest.mock("../dialogs");

@changeset-bot
Copy link

changeset-bot bot commented Dec 20, 2021

⚠️ No Changeset found

Latest commit: 4a4df2a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@threepointone
Copy link
Contributor

This can probably be confusing in the future. Suggestion - create a setupFilesAfterEnv.js (https://jestjs.io/docs/configuration#setupfilesafterenv-array) move jest.mock('../dialogs') and confirm.mockImplementation(() => { throw new ... }); into it (so confirm() is mocked for all test files, and probably prompt() too at some point). This removes the need to add jest.mock("../dialogs"); in each file, and mock-confirm.ts contains just the mockConfirm export.

@petebacondarwin
Copy link
Contributor Author

This can probably be confusing in the future. Suggestion - create a setupFilesAfterEnv.js (https://jestjs.io/docs/configuration#setupfilesafterenv-array) move jest.mock('../dialogs') and confirm.mockImplementation(() => { throw new ... }); into it (so confirm() is mocked for all test files, and probably prompt() too at some point). This removes the need to add jest.mock("../dialogs"); in each file, and mock-confirm.ts contains just the mockConfirm export.

Ooh! TIL...

Copy link
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tentatively approving, with a couple of suggestions.

packages/wrangler/src/__tests__/jest.setup.ts Outdated Show resolved Hide resolved
/**
* The expected values for a confirmation request.
*/
export interface ConfirmExpectation {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this need to be exported?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if it "has" to be exported. But since it is used as the type of a parameter of an exported function it seems the right thing to do...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, is this because if someone wants to construct the arguments separately before passing it to mockConfirm/mockPrompt, they'll have the type to use? Ok, I can buy that.

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.
@petebacondarwin petebacondarwin merged commit 03b8851 into cloudflare:main Dec 21, 2021
@petebacondarwin petebacondarwin deleted the confirm-mock-refactor branch December 21, 2021 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants