1
votes

This code is throwing double callback! error followed by:

TypeError: Cannot read property 'status' of undefined at Assertion. (node_modules\chai\lib\chai\core\assertions.js:890:14) at Assertion.ctx.(anonymous function) [as property] (node_modules\chai\lib\chai\utils\addMethod.js:41:25) at Assertion. (node_modules\chai-http\lib\http.js:80:38) at Assertion.ctx.(anonymous function) [as status] (node_modules\chai\lib\chai\utils\addMethod.js:41:25) at tests\unitTest\helloWorld.js:16:37 at Test.Request.callback (node_modules\chai-http\node_modules\superagent\lib\node\index.js:615:12) at ClientRequest. (node_modules\chai-http\node_modules\superagent\lib\node\index.js:567:10) at Socket.socketErrorListener (_http_client.js:269:9) at emitErrorNT (net.js:1269:8)

The error looks random, sometimes it happens sometimes not.

 var chai = require('chai')
        , chaiHttp = require('chai-http');
    chai.use(chaiHttp);

    var assert = require('assert');
    var expect = chai.expect;
    require('should-http');
describe('Array', function() {
    describe('#indexOf()', function() {
        it('should return -1 when the value is not present', function(done) {
            //assert.equal(-1, [1,2,3].indexOf(4));
            //console.log("x")
            chai.request('http://192.168.99.100:8080').get('/hello').end(function (err, res) {
                expect(res).to.have.status(200);
                res.text.should.equal('hello world2');
                done();
            });
        });
    });
});

documentation: https://github.com/chaijs/chai-http

2
Where are you instantiating your server? Typically in a test you want to build/tear down the server during the test, not have it running outside the test itself. Might be a race condition somewhere. - Daniel Lizik
Running it outside, locally - Jerome Ansia

2 Answers

0
votes

I can not see the double callback! error that you mention in the stack trace. Is it possible that you forgot to copy something? To me, feels more that the server timeouts and you get back a timeout error and because you get a timeout, the res var is undefined in the callback and then breaks because you are trying to access undefined var. You can try to increase the timeout of the tests like that:

this.timeout(4000);

and put some console.log as well in the callback to see what it is printing. Also, be sure, if you run multiple tests, that the server does not crash when running other tests, before this one.

0
votes

Turns out that the problem was each time a new version of the file was changed the server restart and sometimes the address/port is not available:

-Error listen: EADDRINUSE :::5858