Reputation: 1798
I'm trying to setup a fake sinon server for testing some requests. In the code below, my callback function never gets called. The test errors out with Error: timeout of 500ms exceeded. Ensure the done() callback is being called in this test.
Why does the callback function not get called immediately?
var request = require('request');
var sinon = require('sinon');
describe('Job gets data', function(){
var server;
beforeEach(function(){
server = sinon.fakeServer.create();
});
afterEach(function(){
server.restore();
});
context('When there is a GET request to /something', function(){
it('will throw an error if response format is invalid', sinon.test(function(done){
server.respondWith('GET', '/something', [200, { "Content-Type": "application/json" }, '{invalid: "data"}']);
request.get('/something', function (err, response, body) {
console.log(response);
console.log(body);
done();
});
}));
});
Upvotes: 0
Views: 1624
Reputation: 82058
You need to call server.respond
to have all of the requests complete. I found this Gist which gives an example.
This is the relevant code.
server.respondWith("GET", "/something",
[200, { "Content-Type": "application/json" },
'{ "stuff": "is", "awesome": "in here" }']);
var callbacks = [sinon.spy(), sinon.spy()];
jQuery.ajax({
url: "/something",
success: callbacks[0]
});
jQuery.ajax({
url: "/other",
success: callbacks[1]
});
console.log(server.requests); // Logs all requests so far
server.respond(); // Process all requests so far
Upvotes: 3