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

Assert that async expects are awaited or returned #54

Closed
SimenB opened this issue Jan 13, 2018 · 7 comments · Fixed by #255
Closed

Assert that async expects are awaited or returned #54

SimenB opened this issue Jan 13, 2018 · 7 comments · Fixed by #255

Comments

@SimenB
Copy link
Member

SimenB commented Jan 13, 2018

valid-expect should check that if there's a rejects or resolves on an expect, that it's either awaited or returned.

Example where we should warn:

test('some test', () => {
  expect(Promise.resolve('hello')).resolves.toBe('hello');
});
@macklinu
Copy link
Collaborator

I'm going to give this one a try today.

@macklinu
Copy link
Collaborator

macklinu commented Feb 13, 2018

A question about await syntax - is this valid?

test('some test', async () => {
  expect(await Promise.resolve('hello')).resolves.toBe('hello');
});

macklinu added a commit to macklinu/eslint-plugin-jest that referenced this issue Feb 13, 2018
@SimenB
Copy link
Member Author

SimenB commented Feb 14, 2018

is this valid?

Nope, correct there is no resolves as it's already awaited

macklinu added a commit to macklinu/eslint-plugin-jest that referenced this issue Feb 15, 2018
macklinu added a commit to macklinu/eslint-plugin-jest that referenced this issue Feb 16, 2018
macklinu added a commit to macklinu/eslint-plugin-jest that referenced this issue Feb 24, 2018
macklinu added a commit to macklinu/eslint-plugin-jest that referenced this issue Mar 9, 2018
@macklinu
Copy link
Collaborator

I closed #78 - this is open for someone to give another shot at resolving this issue. 👍

@callumlocke
Copy link

This would be a great feature, would have saved me a lot of mistakes today.

It would be nice if it could also be configurable, to enforce one or the other syntax:

// return
test('some test', () =>
  expect(Promise.resolve('hello')).resolves.toBe('hello');
);

// async/await
test('some test', async () => {
  await expect(Promise.resolve('hello')).resolves.toBe('hello');
});

Personally I would want to configure it to allow only await (even in functions with just a single assertion) because it's more explicit. With implicit return it's more likely someone (me) might come along later and add braces in order to add a second assertion, and inadvertently break it.

@SimenB
Copy link
Member Author

SimenB commented Aug 14, 2018

I agree it would be best if we could enforce await over .then or returning the promise

@SimenB
Copy link
Member Author

SimenB commented Jul 20, 2019

🎉 This issue has been resolved in version 22.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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