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

Allow --async-only to be satisfied by returning a promise #1490

Closed
wants to merge 1 commit into from

Conversation

jlai
Copy link
Contributor

@jlai jlai commented Jan 7, 2015

The --async-only flag is intended to catch programmer error where omitting the done callback from the test function arguments causes it to become a synchronous test instead, that does nothing. However, it complains if the test returns a promise, which is unhelpful/unusable when using promises or a mix of async (callback) functions and promises.

This PR proposes changing the behavior of --async-only so that it only fails if the function does not accept a done callback or return a promise. This makes the flag even more useful since forgetting to return a promise will now report an error when using --async-only.

This results in a slight change to the behavior of --async-only: instead of failing immediately for synchronous functions, check to see if the test function returned a promise (or otherwise failed due to an assert, etc) before complaining about not having a done callback.

This results in a slight change to the behavior of --async-only:
instead of failing immediately, check to see if the test returned
a promise (or otherwise failed) before complaining about not
having a done callback.
@aheckmann
Copy link
Contributor

+1

@dasilvacontin
Copy link
Contributor

I would consider this a breaking change since return value was previously disregarded. Specially since some languages than compile into JS (eg CoffeeScript) automatically return in the last line of functions.

@dasilvacontin
Copy link
Contributor

+1 from me.

@danielstjules
Copy link
Contributor

+1 from me as well :)

@dasilvacontin I might be entirely wrong here, but I'm thinking this would only require a minor revision bump. Maybe even just a patch.

It doesn't seem to be breaking backwards compatibility for languages like coffeescript as it only affects the result of returning a promise when using the option. Any javascript/coffescript test that returned a value other than a promise will continue to get flagged when using --async-only. Any test that returned a promise, however, will no longer be incorrectly labeled as not accepting a callback - since they do (

mocha/lib/runnable.js

Lines 267 to 275 in ca84810

if (result && typeof result.then === 'function') {
self.resetTimeout();
result
.then(function() {
done()
},
function(reason) {
done(reason || new Error('Promise rejected with no or falsy reason'))
});
) I'd see this as fixing a bug.

@danielstjules
Copy link
Contributor

@mochajs/owners any thoughts on this?

@dasilvacontin
Copy link
Contributor

It doesn't seem to be breaking backwards compatibility for languages like coffeescript as it only affects the result of returning a promise when using the option.

True, I think I didn't look at the code enough. LGTM. minor?

Reminder to update documentation if this gets merged.

@boneskull
Copy link
Contributor

@mochajs/mocha

OK, I've created branch v2.3.0 for the next major minor, and merged this via 7be4891.

@boneskull boneskull closed this Mar 31, 2015
@dasilvacontin
Copy link
Contributor

next major

minor?

@boneskull
Copy link
Contributor

@dasilvacontin minor 😄

@dasilvacontin
Copy link
Contributor

:shipit:

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

Successfully merging this pull request may close these issues.

5 participants