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

[valid-expect] include fixer for adding missing await #1140

Open
jayu opened this issue Jun 2, 2022 · 3 comments
Open

[valid-expect] include fixer for adding missing await #1140

jayu opened this issue Jun 2, 2022 · 3 comments

Comments

@jayu
Copy link

jayu commented Jun 2, 2022

How do you feel about making jest/valid-expect rule fixable for cases where await is missing ?

So adding await in front of expect in such cases:

it('should', async () => {
   expect(someAsyncThing).rejects.toBe(whatever)
})

//or 

it('should', async () => {
   expect(someAsyncThing).resolves.toBe(whatever)
})

Fixed code then would look like

it('should', async () => {
   await expect(someAsyncThing).rejects.toBe(whatever)
})

//or 

it('should', async () => {
   await expect(someAsyncThing).resolves.toBe(whatever)
})

I'm happy to give it a try if there is a chance it will be merged :)

@G-Rath
Copy link
Collaborator

G-Rath commented Jun 2, 2022

Yup sounds good - we probably should have it as a suggestion if the function isn't already async.

@G-Rath G-Rath changed the title Make jest/valid-expect rule fixable [valid-expect] include fixer for adding missing await Jun 2, 2022
@y-hsgw
Copy link
Contributor

y-hsgw commented May 3, 2024

@G-Rath As for the remaining tasks for this issue, would it be appropriate to suggest that if the target for validation is not async?

e.g:

it('should', async () => {
  const someAsyncThing = () => { ... }
  expect(someAsyncThing).rejects.toBe(whatever)
})

@G-Rath
Copy link
Collaborator

G-Rath commented May 3, 2024

@y-hsgw if you mean suggesting adding await regardless of if async is present, then yeah that's probably fine; though I think we should be able to successfully add async to at least some patterns.

Broadly speaking the logic should be something like:

  1. find the closest function expression upwards in the tree (I'm pretty sure that's what findFirstAsyncFunction does exactly except it also checks if its async; but worth double checking)
  2. if it is not async, include making a fixer for making it async
  3. if the line has a return and ... etc (all the current fixer behaviour)
  4. include a fixer for adding await

While it might seem complicated on the face, I'm actually pretty sure we should be able to always accurately know and fix async/await.

I'm happy to do incremental improvement PRs too, so we can aim for addressing the simple cases and then a followup PR for handling more complex ones

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants