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

[Bug] Type definition of .toThrowWithMessage() is broken #472

Closed
sounisi5011 opened this issue Jul 15, 2022 · 1 comment · Fixed by #475
Closed

[Bug] Type definition of .toThrowWithMessage() is broken #472

sounisi5011 opened this issue Jul 15, 2022 · 1 comment · Fixed by #475

Comments

@sounisi5011
Copy link

Bug

  • package version: jest-extended@3.0.1
  • node version: 16.16.0 or 14.19.3 (This bug is caused by a TypeScript type definition and has nothing to do with the Node.js version)
  • pnpm version: 6.33.0

Relevant code

expect(() => {
  throw TypeError('hello world');
}).toThrowWithMessage(TypeError, 'hello world');

expect.toThrowWithMessage(TypeError, 'hello world');

What you did:

I used TypeScript's tsc command to check for the presence of errors caused by type definitions in the test code.

What happened (please provide anything you think will help):

Due to a change introduced in #471, the constructor function cannot be specified as the first argument of toThrowWithMessage. If tried, the following error will be thrown:

error TS2345: Argument of type 'TypeErrorConstructor' is not assignable to parameter of type '(...args: unknown[]) => unknown'.
  Types of parameters 'message' and 'args' are incompatible.
    Type 'unknown' is not assignable to type 'string | undefined'.

3 }).toThrowWithMessage(TypeError, 'hello world');
                        ~~~~~~~~~
error TS2345: Argument of type 'TypeErrorConstructor' is not assignable to parameter of type '(...args: unknown[]) => unknown'.
  Types of parameters 'message' and 'args' are incompatible.
    Type 'unknown' is not assignable to type 'string | undefined'.

5 expect.toThrowWithMessage(TypeError, 'hello world');
                            ~~~~~~~~~

TS Playground link


Do not use (...args: unknown[]) => unknown in the type definition of an argument that can be passed any function.
This type requires that the function passed as an argument must be able to accept arguments of type unknown.
However, arguments of type unknown cannot be passed to error constructor functions, etc.
Use (...args: any[]) => unknown instead.

Or, given the internal process, new (message: string) => unknown seems appropriate.

Note: The truly correct type is new (message: string | RegExp) => unknown. However, when fixed to this type, we cannot specify the constructor function of the Error object. Perhaps this is a bug in .toThrowWithMessage(), but the Error object constructor function in Node.js works even if a RegExp is passed to the message argument, so I don't think there is any hurry to fix it.

@kayahr
Copy link

kayahr commented Jul 31, 2022

So when will this fix be published in a v3.0.2 for example? All my projects use jest-extended and it's a bit annoying to always exclude it from updating dependencies to prevent running into this compilation error.

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 a pull request may close this issue.

2 participants