I am using mocha to test if some routes I have added to my server are returning 200 ok and if the response contains a string. I am doing this it the following manner:
var testDBRoute = function(link, routeName) {
it('should return 200 OK for ' + routeName, function (done) {
http.get(link, function (res) {
assert.equal(200, res.statusCode);
done();
})
});
it('should return the correct info',
function(done) {
http.get(link, function (res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var response = JSON.parse(body);
assert.equal('abc', response.id);
done();
});
});
}
)
};
I am calling this function 6 times in the same way:
describe('/link1', function () {
var link = HOST + '/api/link1/';
testDBRoute(link, 'link1');
});
No matter the order of the requests it always fails at the 8'th test meaning that it's making 8 http get requests and then fails with this error:
Error: timeout of 2000ms exceeded at null. (\\node_modules\\mocha\\lib\\runnable.js:158:19) at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
I must specify that I am running the test on windows.
I have solved the issue using superTest to replace http like this
var request = require('supertest');
var app = require('../server');
// app is the express context from the server
describe('GET /api', function () {
it('test api', function (done) {
request(app)
.get('/api')
.end (function (err, res) {
assert.equal(200, res.statusCode);
done();
});
});
});
So it seems like the problem is in the http module...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.