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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
馃殌 Feature: New test resolution behavior #2509
Comments
I think it looks great! I think #2511 is related, since we're talking about how Mocha will respond with |
I'm tagging this |
I'm not super excited to include it as a dependency, but asCallback and/or fromCallback would likely simplify the implementation. |
If we add waiting for two async operations at the same time I think we need to improve the timeout handling as well. The timeout message should tell you which one of the async blockers timed out, or if both of them did. Otherwise it gets really hard to debug |
@Munter agree |
Just copy/pasting this comment here: Example of using both it('fires change event when calling blah.doSomethingThatFiresChange', async function (done) {
const blah = await getBlah()
blah.on('change', _ => done())
blah.doSomethingThatFiresChange()
}) 1) fires change event when calling blah.doSomethingThatFiresChange:
Error: the `done` callback was successfully called, but the returned `Promise`
was rejected with the following `TypeError`:
TypeError: Cannot read property 'likes' of undefined
at Blah.doSomethingThatFiresChange (lib/Blah.js:5:9)
at Context.<anonymous> (test/Blah.spec.js:4:8)
More info: https://github.com/mochajs/mocha/wiki/some-page |
What happens if something other than a promise is returned? |
@tandrewnichols It would get ignored, as it currently does. |
I actually want to weigh in against this; all of my ever-growing experience points toward mixing of promises and callbacks being worse than either one, and I think I just figured out why.
(NB: to the extent that TL;DR: It's wrong except when it's "right" in the sense of being identical in effect to a superior alternative that doesn't have the high probability of being wrong. I realize that's an unusually categorical judgement for programming, art of tradeoffs, but I am fairly sure the logic above guarantees it. Strong opinions weakly held and all that -- if there's a flaw in the logic I'll gladly reconsider either the logic or my stance on mixing these paradigms. As always, just my two cents' worth. 馃槈 |
@ScottFreeCode I've finally read through your message, but I either don't understand it or I disagree. In my head, the solution would be like, okay, I'm the runner, I've got the callback let callback
const callbackP = new Promise((resolve, reject) => {
callback = function (err) {
if (err) reject(err)
else resolve()
}
} And then we merge both promises to know when the whole thing ends: const wholeThingP = Promise.all(callbackP, testP) Whenever I don't feel like the "there's risk in doing this" argument is justified. Most likely because it doesn't feel complicated to me, and/or the solution I have in mind is different. Feels nice to be engaging in the community after so long! 馃槉 |
@dasilvacontin scott's long gone unfortunately |
Now that If anybody has a good reason to change this core behavior, please do file an issue with one of our new issue templates. We'd be happy to take a look. Cheers all! 鉂わ笍 |
See #2413 and other various issues and PRs for previous discussion.
In Mocha 2.x, when a
Promise
is returned from an "async" test (using thedone()
callback), thePromise
resolution is effectively ignored, though it may cause an unhandled rejection after the test has finished.In Mocha 3.x to the present, returning a
Promise
from an "async" test is disallowed; tests must use eitherdone()
or return aPromise
, but not both.A better solution in this case would be to wait for both resolutions (the call of
done()
andPromise
fulfillment) before the test can end.Specification
When a test requests
done()
and returns aPromise
:done()
is calledPromise
has been resolveddone()
is called to conclude the testdone()
is called with an error parameterdone()
is called without an error parameterPromise
has been rejecteddone()
is called to conclude the testdone()
is eventually called, it should be ignoreddone()
was called, any error parameter would have been ignoreddone()
is called before it is fulfilleddone()
is not called with an error parameterPromise
fulfillment to conclude the testPromise
has been rejectedPromise
has been resolveddone()
is called with an error parameterPromise
fulfillment to conclude the testPromise
fulfills, it should be ignoredPromise
has been rejected, any error parameter would have been ignoredNormal timeout rules apply.
Comments, suggestions, questions, concerns?
The text was updated successfully, but these errors were encountered: