-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
How to avoid false positives now? #2511
Comments
// passes even with --async-only
it("function with no assertions", function(done) {
setTimeout(done, 1000)
}) All that // ! oops, Mocha thinks this test passed once the function returns
it("invalid asynchronous test", function() {
setTimeout(function() { // (more realistically, could be an asynchronous API with result passed to callback)
assert(whatever) // ! Mocha doesn't know to wait for the callback
}, 1000)
})
// ! ok, Mocha now knows to wait for `done` to be called
it("valid asynchronous test", function(done) {
setTimeout(function() { // (more realistically, could be an asynchronous API with result passed to callback)
assert(whatever)
done() // ! Mocha knows the test is complete at this point
}, 1000)
}) But promises accomplish the same thing because Mocha waits for the promise to resolve or reject: it("valid asynchronous test", function() {
return new Promise(function(resolve) {
setTimeout(function() { // (more realistically, the promise might come from a promise API)
assert(whatever) // if this throws, the promise becomes rejected
resolve()
}, 1000)
})
}) And it("hypothetical promise-based test", function() {
return somePromiseAPI("what data do we want")
.then(function(data) {
assert(data)
})
})
it("equivalent async-function test", async function() {
var data = await somePromiseAPI("what data do we want")
assert(data)
}) |
It's true that you can certainly write useless tests with In this example, the assertion is only being tested if the
With
The problem is that |
So if I'm understanding this right, you're testing a mix of promises and callbacks, and need to finish the test in the callback rather than the promise, and |
The other issue was #2494; it's unfortunate niether of these opens with the real problem situation of testing callbacks with One basic approach would be similar to the potential solution I suggested there: don't use I doubt that Mocha's going to have any reliable, cross-environment way to detect async functions (unless there's a standards-mandated way of identifying them that I'm not yet aware of), so we probably won't be able to do anything special for that. And the general case that Maybe we just need a new, distinct flag to require |
Yeah, I'd be fine with something like a I do think #2509 is a separate issue in that it deals with explicitly allowing both to exist. |
I'm not sure why Labeling |
I am a bot that watches issues for inactivity. |
Back on #621, the
--async-only
feature was created to force developers to usedone
as a work-around to mocha not knowing anything about the assertion libraries. It provided a way for devs to ensure their assertions were hit.It looks like this functionality is now lost when returning promises (See #1490, #1636), which hurts when using Babel's awesome async-to-generator plugin. With the
--async-only
flag on, false positives are possible again as you're no longer forced to usedone
with it enabled.Thoughts?
The text was updated successfully, but these errors were encountered: