-
-
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
Introduce a lint rule to report error when testing promises. If a exp… #4844
Conversation
…ectation is added inside a then or catch block of promise then as per jest docs, it is mandatory to return that promise for succesfull execution of that expectation. This rull will report all such expectations where promise is not returned.
Codecov Report
@@ Coverage Diff @@
## master #4844 +/- ##
=========================================
Coverage ? 59.34%
=========================================
Files ? 201
Lines ? 6698
Branches ? 4
=========================================
Hits ? 3975
Misses ? 2723
Partials ? 0
Continue to review full report at Codecov.
|
Fixing typo in changelog.
I'm not sure if this is the correct fix for the problem. I think this issue is more generally solved using something like https://www.npmjs.com/package/babel-jest-assertions? It will only fail when running the test instead of telling you inline in the IDE. But it will cover more cases (like if the A lint rule might be to always expect a |
@SimenB thanks for letting me know about that super-cool plugin 👍
Here I was able to see "The expect will be executed" getting printed on console. And there is no effect on jest reporting, if I change the expectation of 'data' to 'any string' instead of 'Hi'
Here I was able to see "The expect will be executed" getting printed on console. And there was effect on jest reporting, if I change the expectation of 'data' to 'any string' instead of 'Hi'
Here, as the promise is rejected, expect.hasAssertions() will fail the test, since no assertion is executed.
Here, ideally test should fail as value is '10'. But as said earlier this assertion gets executed but jest honors that only when we return that Promise. If I add return to above example test will fail for value not be 1 In the end, this lint rule looks to be required even if you use that plugin, as I discovered today that, assertion may or may not get executed but jest will honor result on that assertion only if return statement is added. @SimenB @cpojer @thymikee please let me know if my understanding is correct. |
As per above analysis, combination of this lint rule + this plugin would make it more seamless,
|
@tushardhole I could be wrong here but I think the issue you are seeing is because you aren't marking the callback (test function) as asynchronous with either describe('async', () => {
const getData = () => Promise.resolve('Hello');
it('should fail as hello is returned when getting data (using done callback)', (done) => {
getData().then((data) => {
console.log("The expect will be executed")
expect(data).toEqual("Hi");
done();
});
});
it('should fail as hello is returned when getting data (using async/await)', async () => {
await getData().then((data) => {
console.log("The expect will be executed")
expect(data).toEqual("Hi");
});
});
}); |
@mattphillips that example works. To conclude this as per my understanding, the plugin will work for this this issue, if the test is written using either async/await or providing a done. In case a developer do not do this, which is possible as human error, this lint rule would be still helpful. I do not want to be biased to get this PR merged, I feel still it makes sense to to have this lint rule and I would be interested to hear feedback from the community. 😄 If we all feel this lint rule is not required and the plugin with async/await is the way to go ahead, then no issues to close the PR. |
We've decided to move the linting package out of this repo, and into https://github.com/jest-community/eslint-plugin-jest. Could you please open this PR against that repo instead? Thanks! |
For people following along: jest-community/eslint-plugin-jest#42 |
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. |
…ectation is added inside a then or catch block of promise then as per jest docs, it is required to return that promise for successful execution of that expectation. This rule will report all such expectations where promise is not returned.
Summary
Implements #4843
As jest user I work on a project that heavily uses promises and jest is our tool to write tests. We were aware that as per jest docs, it is required to return a promise, if you want to test some thing in its fulfillment or rejection block.
If you do not return that promise and write some exceptions in its then or catch block. Then that test will always be green, as the expectation in then or catch block is never executed. If you return that promise then the expectation will be executed and tests will be red or green depending on the actual result of that expectation.
In this situation it becomes very important to make sure that,
a) You make that test red before making it green for the final push. Which will assure that the exception is getting executed.
As human, we may forget to do that and hence this lint rule will be helpful.
Test plan
Limitations