-
-
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
Async test fails with timeout instead of assertion error #1128
Comments
Does |
@NickHeiner Yes, it resolves; and then |
@gurdiga how do you know it throws if you get a timeout and not an assertion error? |
@hallas @NickHeiner Here is the running thing: http://jsfiddle.net/gurdiga/p9vmj/. |
@gurdiga it seems to me that your promise has its own error catching. Try to add a |
@hallas Wow: that was the answer! :) Thank you! |
I feel stupid. I think I finally got it: when anything throws in any of the promise’s handler functions, be it the one passed to To get out of the promise, I use it('returns the correct value', function(done) {
var returnValue = 5;
aPromise.then(function() {
setTimeout(function() {
expect(returnValue).to.equal(42);
done();
});
});
}); I found this is the only way to get proper error messages and test runner behavior. With |
I ran into a similar problem, and eventually realized you shouldn't use done when testing asynchronous functions with promises, instead, just return the promise. So you should be able to do this without the timeout, e.g.: it('returns the correct value', function() {
var returnValue = 5;
return aPromise.then(function() {
expect(returnValue).to.equal(42);
});
}); |
I think that this issue still exists. I'm getting a timeout issue that cannot be resolved by returning a promise because my module does not use promises. it("works", function(done) {
new Something()
.on("eventA", function(result) {
expect(result).to.be.true;
})
.on("eventB", function(result) {
expect(result).to.be.false;
done();
});
});
Ideas: |
As regards the blog post's recommendations, I don't know about async function error handling, but for plain promises the try-catch recommendation is weird: the original promise attempt with no try-catch was nearly correct, it only needed to use As for your API:
|
|
Sorry, I still don't know how your API is supposed to report errors. |
Yeah, so far I've just been relying on the timeouts for when somethings fails, then manually digging to find out how/why. Fortunately, things rarely break, but that's not an excuse for the lack of a better design (on my part, mostly). |
@stevenvachon: Forgive me in advance, but I don't see an immediate issue with your example. The assertions made in your event listeners should be handled by Mocha via the Now if your implementation under the hood is using Promises, but emits events rather than exposes the Promise, your assertions will indeed be "eaten". The way I get around this problem is to use I usually put this in a setup script that is run before my tests: process.on('unhandledRejection', function (reason)
{
throw reason;
}); Note: This may need some extra elbow grease to work in browsers. I hope to to see Mocha support this like it does |
Having the same issue with [email protected] it('Convert files into base64', (resolve) => {
let files = Promise.all(promises);
return files
.then(([actual, expected]) => {
assert.equal(actual, expected, 'Files not are equal');
resolve();
})
.catch(error => resolve);
});
|
The (Also, idiomatically, However, in this case you can simplify it even further by just returning the promise and not using the test done parameter at all, since Mocha will wait for the promise to succeed or fail as indicating test success or failure (provided there's no done parameter to the test function; behavior in the event both are used is still being hashed out): it('Convert files into base64', () => {
let files = Promise.all(promises);
return files
.then(([actual, expected]) => {
assert.equal(actual, expected, 'Files not are equal');
})
}); |
@lsphillips that works for me. Thanks!! I hope to see mocha support this by default too. I just created #2640. |
Took me a while to work this out! Based on answers above, these are the two options:
// test.spec.js
var $q = require('q');
var expect = require('expect.js');
describe('tests with done', function(){
it('returns the correct value from promise', function(done) {
var returnValue = 5;
var def = $q.defer();
def.promise.then((val) => {
expect(val).to.equal(42);
done();
}).catch(done);
def.resolve(returnValue)
});
})
describe('tests returning promises', function(){
it('returns the correct value from promise', function() {
var returnValue = 5;
var def = $q.defer();
def.resolve(returnValue)
return def.promise.then((val) => {
expect(val).to.equal(42);
});
});
}) tests with done
1) returns the correct value from promise
tests returning promises
2) returns the correct value from promise
0 passing (15ms)
2 failing
1) tests with done returns the correct value from promise:
Error: expected 5 to equal 42
at Assertion.assert (node_modules/expect.js/index.js:96:13)
at Assertion.be.Assertion.equal (node_modules/expect.js/index.js:216:10)
at def.promise.then (tests/test.spec.js:9:24)
at _fulfilled (node_modules/q/q.js:854:54)
at self.promiseDispatch.done (node_modules/q/q.js:883:30)
at Promise.promise.promiseDispatch (node_modules/q/q.js:816:13)
at node_modules/q/q.js:570:49
at runSingle (node_modules/q/q.js:137:13)
at flush (node_modules/q/q.js:125:13)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
2) tests returning promises returns the correct value from promise:
Error: expected 5 to equal 42
at Assertion.assert (node_modules/expect.js/index.js:96:13)
at Assertion.be.Assertion.equal (node_modules/expect.js/index.js:216:10)
at def.promise.then (tests/test.spec.js:22:24)
at _fulfilled (node_modules/q/q.js:854:54)
at self.promiseDispatch.done (node_modules/q/q.js:883:30)
at Promise.promise.promiseDispatch (node_modules/q/q.js:816:13)
at node_modules/q/q.js:570:49
at runSingle (node_modules/q/q.js:137:13)
at flush (node_modules/q/q.js:125:13)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
|
@gurdiga Thanks for the setTimeout() idea! I had a similar problem, but now I can get proper error messages atleast! |
In my scenario I used Nightmare to end2end tests. The solution for me was use describe('Clicking in any bad reputation tag', () => {
it('open the bad reputation modal', (done) => {
nightmare
.select('#per-page', '50')
.waitForAjax()
.click('[data-reputation="bad"]')
.evaluate(function() {
return document.querySelector('.vue-modal .ls-modal-title').innerText
})
.then(function(title) {
title.should.equal('Sua segmentação teve uma avaliação ruim!')
done()
})
.catch((error) => {
screenshot(nightmare)
done(error)
})
})
}) |
@itumoraes that works, but you can do this instead: describe('Clicking in any bad reputation tag', () => {
it('open the bad reputation modal', () => {
return nightmare
.select('#per-page', '50')
.waitForAjax()
.click('[data-reputation="bad"]')
.evaluate(function() {
return document.querySelector('.vue-modal .ls-modal-title').innerText
})
.then(function(title) {
title.should.equal('Sua segmentação teve uma avaliação ruim!')
})
})
}) You don't need to call |
i used the below script but i got the same Timeout exceed error. Myscript : describe("getBillingDetail", async function (){ Error: Timeout of 55000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. |
Stop spelling the same thing in multiple closed issues. Don't pass a done callback into async functions. Read the documentation on async tests |
@Munter i removed the done callback,but these error is occur again |
It looks like that your promise never resovled. |
this is a pretty solution. thanks |
I just had issues with this as well;
would timeout with Wrapping it in a normal function and returning the async function/promise instead worked:
or
|
Perfect for async/await and express ! |
This test:
This test fails with
timeout of 2000ms exceeded
instead of assertion error. I guess that’s becauseexpect()
call throws an error, and thedone()
never gets executed, and I’m wondering if there is a better way to test this kind of code.The text was updated successfully, but these errors were encountered: