Mocha Tests Using Superagent + Promises Timeout Rather Than Fail With 'expect'
I'm using mocha to run a number of integration tests against an external web service. I use superagent-promise for the request/response handling, and I'm using expect as my asserti
Solution 1:
Is this a known issue?
This is actually not an issue.
The problem is that expect(err.status).toBe(200)
throws an error that is swallowed inside the .then
and which causes the code to never reach done()
. You should restructure you code to the following:
it('[MESSAGES-1] cannot be posted without an auth token', function(done) {
agent.post(config.webRoot + '/rooms/ABC/messages').send({
content: 'This is a test!'
}).end()
.then(function(res) {
// Not expected
}, function(err) {
expect(err.status).toBe(401)
done()
})
.catch(function(err) {
done(err); //report error thrown in .then
})
})
This way you catch and report the error thrown by expect(err.status).toBe(200)
.
Solution 2:
In your case the timeout happens becouse the done callback is never called, either because the http request didnt fail, or the expectation failed so it threw an assertation error.
Mocha handles proper (promise returning) asynchronous tests, so don't use the done callback, it causes confusion when mixed with promises. Return the promise instead:
it('[MESSAGES-1] cannot be posted without an auth token', function() {
return agent.post(config.webRoot + '/rooms/ABC/messages').send({
content: 'This is a test!'
}).end().then(function(res) {
// here you must throw an error, because if the post didnt fail somehow, the test would be green because of no assertations and no promise rejection.
throw new Error("Not expected");
}, function(err) {
expect(err.status).toBe(401);
});
});
Post a Comment for "Mocha Tests Using Superagent + Promises Timeout Rather Than Fail With 'expect'"