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

[jest] fix type inference for Mocked & mockResolvedValue/mockRejectedValue #32816

Merged
merged 4 commits into from Feb 12, 2019

Conversation

antoinebrault
Copy link
Contributor

@antoinebrault antoinebrault commented Feb 6, 2019

Follow-up of #32579 (comment) , fixes #32890

Fixed Mocked interface & mockResolvedValue/mockRejectedValue

Breaking Change

With types inferred, mockResolvedValue(), mockRejectedValue() will not compile if they are used on a function not returning a Promise.
e.g.

class Test {
    method(x: string): string {
        return x;
    }
}
const test: Mocked<Test> = new Test() as any;

// this doesn't compile, because mockImplementation expects () => string
test.method.mockImplementation(() => Promise.resolve(''));

// this shouldn't compile either because mockResolvedValue() is sugar for mockImplementation(() => Promise.resolve());
test.method.mockResolvedValue('');

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Add or edit tests to reflect the change. (Run with npm test.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

Select one of these and delete the others:

If changing an existing definition:

  • Provide a URL to documentation or source code which provides context for the suggested changes: private project
  • Increase the version number in the header if appropriate.
  • If you are making substantial changes, consider adding a tslint.json containing { "extends": "dtslint/dt.json" }.

@antoinebrault
Copy link
Contributor Author

@Chris-V updated code to be consistent with mockImplementation() & added tests

@typescript-bot typescript-bot added this to Waiting for Reviewers in Pull Request Status Board Feb 6, 2019
@typescript-bot typescript-bot added Popular package This PR affects a popular package (as counted by NPM download counts). Awaiting reviewer feedback labels Feb 6, 2019
@typescript-bot
Copy link
Contributor

typescript-bot commented Feb 6, 2019

@antoinebrault Thank you for submitting this PR!

🔔 @NoHomey @jwbay @asvetliakov @alexjoverm @epicallan @ikatyang @wsmd @JamieMason @douglasduteil @ahnpnl @JoshuaKGoldberg @UselessPickles @r3nya @Hotell @sebald @andys8 @aldentaylor - please review this PR in the next few days. Be sure to explicitly select Approve or Request Changes in the GitHub UI so I know what's going on.

If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead.

@Chris-V
Copy link

Chris-V commented Feb 6, 2019

Thank you! We had several helpers types in our code base to improve type-safety with jest. This will help us get rid of most of them!

@fgrandel
Copy link

fgrandel commented Feb 8, 2019

I provided an alternative proposal to solve this issue just for consideration, see #32898 - Please feel free to close this PR if you prefer your own solution. :-)

Copy link

@fgrandel fgrandel left a comment

Choose a reason for hiding this comment

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

Proposal to add additional tests + (optional) proposal to introduce correct (i.e. more strongly typed) signature for jest.fn, Mock, etc. Feel free to go along w/o the proposed changes if you think I'm off track. :-)

types/jest/index.d.ts Show resolved Hide resolved
types/jest/jest-tests.ts Show resolved Hide resolved
@typescript-bot typescript-bot moved this from Waiting for Reviewers to Needs Author Attention in Pull Request Status Board Feb 8, 2019
@typescript-bot typescript-bot added Revision needed This PR needs code changes before it can be merged. and removed Awaiting reviewer feedback labels Feb 8, 2019
@typescript-bot
Copy link
Contributor

typescript-bot commented Feb 8, 2019

@antoinebrault Unfortunately, this pull request currently has a merge conflict 😥. Please update your PR branch to be up-to-date with respect to master. Have a nice day!

types/jest/index.d.ts Outdated Show resolved Hide resolved
@typescript-bot
Copy link
Contributor

🔔 @Jerico-Dev @jwbay - Thanks for your review of this PR! Can you please look at the new code and update your review status if appropriate?

@typescript-bot typescript-bot added the Other Approved This PR was reviewed and signed-off by a community member. label Feb 11, 2019
@typescript-bot
Copy link
Contributor

🔔 @jwbay - Thanks for your review of this PR! Can you please look at the new code and update your review status if appropriate?

@typescript-bot typescript-bot moved this from Needs Author Attention to Check and Merge in Pull Request Status Board Feb 12, 2019
@typescript-bot typescript-bot added the Owner Approved A listed owner of this package signed off on the pull request. label Feb 12, 2019
@typescript-bot
Copy link
Contributor

A definition owner has approved this PR ⭐️. A maintainer will merge this PR shortly. If it shouldn't be merged yet, please leave a comment saying so and we'll wait. Thank you for your contribution to DefinitelyTyped!

@typescript-bot typescript-bot moved this from Check and Merge to Needs Author Attention in Pull Request Status Board Feb 12, 2019
@typescript-bot typescript-bot added Has Merge Conflict This PR can't be merged because it has a merge conflict. The author needs to update it. and removed Merge:Express labels Feb 12, 2019
# Conflicts:
#	types/jest/index.d.ts
@typescript-bot typescript-bot moved this from Needs Author Attention to Check and Merge in Pull Request Status Board Feb 12, 2019
@typescript-bot typescript-bot added Merge:Express and removed Has Merge Conflict This PR can't be merged because it has a merge conflict. The author needs to update it. labels Feb 12, 2019
antoinebrault referenced this pull request Feb 12, 2019
…roperties & spyOn method to function properties
@minestarks minestarks merged commit d29109f into DefinitelyTyped:master Feb 12, 2019
@typescript-bot
Copy link
Contributor

I just published @types/jest@24.0.2 to npm.

@typescript-bot
Copy link
Contributor

I just published @types/jest-when@1.1.4 to npm.

@typescript-bot typescript-bot removed this from Check and Merge in Pull Request Status Board Feb 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Other Approved This PR was reviewed and signed-off by a community member. Owner Approved A listed owner of this package signed off on the pull request. Popular package This PR affects a popular package (as counted by NPM download counts). Revision needed This PR needs code changes before it can be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

jest: Regression: mock{Resolved|Rejected}... no longer has the correct return type
6 participants