-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Conversation
toThrow()
and toThrowError()
toThrow()
and toThrowError()
matchers
Codecov Report
@@ 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.
|
packages/expect/src/types.ts
Outdated
@@ -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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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: https://github.com/facebook/jest/blob/f13abff8df9a0e1148baf3584bcde6d1b479edc7/packages/expect/src/toThrowMatchers.ts#L144-L147
(should remove one of them...)
I'm actually working on typing the entire |
@mrazauskas also, could you add a test? Sorta like https://github.com/facebook/jest/pull/11931/files#diff-63545bc97334ebcdd928dfcccddf0afd945a7c2a797fb0fc5c499b3fd4184439 |
Sure. I will add a test too. |
You run the type tests via |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
By the way, if it is not overlapping, I could add type tests for other matchers too. That was fun. |
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 🙂) |
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. |
Resolves #10087
Summary
It looks like the typings issue described in #10087 is caused by definition of
Constructable
inexpect
package – https://github.com/facebook/jest/blob/f13abff8df9a0e1148baf3584bcde6d1b479edc7/packages/expect/src/types.ts#L93-L95In
@types/jest
it is defined differently:Changing
unknown
s back intoany
s solves the issue.It is possible to revert back or to move forward and to use
ErrorConstructor
instead ofConstructable
. This solves the problem as well and is more explicit, right?Test plan
Added tests. As usual.