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

Add no-synchronous-tests rule #26

Merged
merged 3 commits into from
Aug 19, 2015
Merged

Conversation

2fast2fourier
Copy link
Contributor

I've had a need for a rule based on my experience writing a bunch of async tests within mocha. This rule checks that either a callback was specified or that a promise was returned in the test function.

Mocha dynamically determines whether a test is synchronous by whether the function includes a callback or returns a promise. Because of that, it's easy to end up in a situation where you intend to return a promise (and do not have a done callback) but accidentally omit the return statement. This leads to a false-positive where the test would automatically succeed regardless of the actual async result.

This rule works well side-by-side with handle-done-callback, as that rule will enforce whether or not a callback was actually called.

Let me know if you see any issues with this PR and I'll try to resolve them. Thanks in advance!

@lo1tuma
Copy link
Owner

lo1tuma commented Aug 13, 2015

Thanks for the PR.

Can you explain why a eslint rule would be better instead of using the --async-only option from mocha? IMHO the only advantage would be to detect synchronous tests earlier through static analysis while the --async-only option would make synchronous tests fail during the test-run. Is there any other advantage?

@phated
Copy link
Contributor

phated commented Aug 18, 2015

The --async-only option doesn't work for returning promises in the current stable release. It has been fixed in master (see mochajs/mocha#1490) but there is no timeline for when that will see a release. It is also just nice to catch this during the linting process (especially seeing it in your editor)

@2fast2fourier
Copy link
Contributor Author

Sorry, I thought I replied earlier and just realized it didn't go through. Thanks for bringing up --async-only, I wasn't aware of it until now. I think this rule still has value as it would allow for someone to catch a mistake before they attempt to run the tests, I find sublime eslint integration to be a big timesaver.

@lo1tuma
Copy link
Owner

lo1tuma commented Aug 19, 2015

Thanks for the explanation, sounds reasonable to me 😉.

}

if (!returnStatement) {
context.report(functionExpression, 'Expected test to handle a callback or return a promise.');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error message could be improved. The rule doesn’t check if a callback is handled, so this is not quite correct. What do you think about "Unexpected synchronous test."?

@2fast2fourier
Copy link
Contributor Author

I've added those test cases and updated the messaging. Thanks for the feedback!

@lo1tuma
Copy link
Owner

lo1tuma commented Aug 19, 2015

LGTM.

Thanks for your work 🍻.

lo1tuma added a commit that referenced this pull request Aug 19, 2015
@lo1tuma lo1tuma merged commit 2b450cb into lo1tuma:master Aug 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants