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

fix(expect): improve typings of toThrow() and toThrowError() matchers #11929

Merged
merged 1 commit into from Oct 5, 2021

Conversation

@mrazauskas
Copy link
Contributor

@mrazauskas mrazauskas commented Oct 5, 2021

Resolves #10087

Summary

It looks like the typings issue described in #10087 is caused by definition of Constructable in expect package –

interface Constructable {
new (...args: Array<unknown>): unknown;
}

In @types/jest it is defined differently:

interface Constructable {
  new (...args: any[]): any;
}

Changing unknowns back into anys solves the issue.

It is possible to revert back or to move forward and to use ErrorConstructor instead of Constructable. This solves the problem as well and is more explicit, right?

Test plan

Added tests. As usual.

@mrazauskas mrazauskas changed the title fix(expect): improve typings of toThrow() and toThrowError() fix(expect): improve typings of toThrow() and toThrowError() matchers Oct 5, 2021
@codecov-commenter
Copy link

@codecov-commenter codecov-commenter commented Oct 5, 2021

Codecov Report

Merging #11929 (245dbbb) into main (f13abff) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #11929   +/-   ##
=======================================
  Coverage   68.75%   68.75%           
=======================================
  Files         322      322           
  Lines       16589    16589           
  Branches     4786     4786           
=======================================
  Hits        11405    11405           
  Misses       5152     5152           
  Partials       32       32           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f13abff...245dbbb. Read the comment docs.

Loading

@@ -306,11 +302,11 @@ export interface Matchers<R> {
/**
* Used to test that a function throws when it is called.
*/
toThrow(error?: string | Constructable | RegExp | Error): R;
toThrow(error?: string | RegExp | Error | ErrorConstructor): R;
Copy link
Collaborator

@SimenB SimenB Oct 5, 2021

Choose a reason for hiding this comment

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

this should probably be just be just error?: unknown for the same reason TS has made stuff in catch unknown by default in strict mode: https://devblogs.microsoft.com/typescript/announcing-typescript-4-4/#use-unknown-catch-variables

Loading

Copy link
Collaborator

@SimenB SimenB Oct 5, 2021

Choose a reason for hiding this comment

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

same below, the implementation is identical:

const matchers: MatchersObject = {
toThrow: createMatcher('toThrow'),
toThrowError: createMatcher('toThrowError'),
};

(should remove one of them...)

Loading

@SimenB
Copy link
Collaborator

@SimenB SimenB commented Oct 5, 2021

I'm actually working on typing the entire expect package better right now. Good timing! 😀 (I haven't touched specific matchers, tho, so no overlap with this PR)

Loading

@SimenB
Copy link
Collaborator

@SimenB SimenB commented Oct 5, 2021

Loading

@mrazauskas
Copy link
Contributor Author

@mrazauskas mrazauskas commented Oct 5, 2021

Sure. I will add a test too.

Loading

@SimenB
Copy link
Collaborator

@SimenB SimenB commented Oct 5, 2021

You run the type tests via yarn test-types in root, btw. In this case a test doesn't matter much, but we should expand our type tests, and this seems like a nice one to have as we build it up 🙂

Loading

@mrazauskas mrazauskas force-pushed the fix-expect-types branch from 245dbbb to e7fc6ad Oct 5, 2021
@mrazauskas mrazauskas requested a review from SimenB Oct 5, 2021
SimenB
SimenB approved these changes Oct 5, 2021
Copy link
Collaborator

@SimenB SimenB left a comment

Thanks!

Loading

@SimenB SimenB merged commit e420155 into facebook:main Oct 5, 2021
23 of 30 checks passed
Loading
@mrazauskas
Copy link
Contributor Author

@mrazauskas mrazauskas commented Oct 5, 2021

By the way, if it is not overlapping, I could add type tests for other matchers too. That was fun.

Loading

@SimenB
Copy link
Collaborator

@SimenB SimenB commented Oct 5, 2021

That'd be awesome! ❤️

I don't have any plans to do more work for the types now (and if I do it'll be how to add custom matchers, not messing with the ones we have 🙂)

Loading

@mrazauskas mrazauskas deleted the fix-expect-types branch Oct 18, 2021
@github-actions
Copy link

@github-actions github-actions bot commented Nov 18, 2021

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Loading

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

4 participants