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

Ensure expect(function).toThrow() #1329

Open
rentalhost opened this issue Jan 15, 2023 · 3 comments
Open

Ensure expect(function).toThrow() #1329

rentalhost opened this issue Jan 15, 2023 · 3 comments

Comments

@rentalhost
Copy link

When is possible to determine if the expect(value) is not a function when used with .toThrow(), it must be an error.

const str = "any value";

expect(str).toThrow() // Not works.
const returnStr = () => "any value";

expect(returnStr).toThrow() // Not handle, but is a valid case (if returnStr() throws an exception).
@SimenB
Copy link
Member

SimenB commented Jan 16, 2023

You get a runtime error (and possibly a type error?) for this already. Not sure if a lint rule is needed?

@rentalhost
Copy link
Author

@SimenB there is no type error, as expect() accepts any. The runtime error occur but is not clear enough for "first-time jesters".


image
No type error.


image
Error itself is clear, but not working as expected, once that I tried to capture it via .toThrow(), but it is thrown before to catch.


In my opinion, eslint-plugin-jest would do a good job of catching this type of error before the user runs it, as it would be able to predict and fix it. Even a --fix would be feasible by applying a function wrapper.

@G-Rath
Copy link
Collaborator

G-Rath commented Jan 29, 2023

Given that TypeScript currently won't catch this, it might be worth a lint rule but it would have to use TypeScript to determine the type of whatever's been given to expect.

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

No branches or pull requests

3 participants