Search code examples
node.jsmocha.jsrequestify

How to test an external HTTP resource using mocha and requestify


I am trying to do a simple test of an external HTTP resource using mocha. Here is my code:

describe('bing.com', function() {
    it('should return 200 for a GET request', function() {
        var requestify = require('requestify');

        requestify.get('http://bing.com')
            .then(function(response) {
                // Get the response body (JSON parsed or jQuery object for XMLs)
                console.log(response.getBody());
                done();
            });
    });
});

The test just says passed but my console.log call is never shown. Is mocha completing before the http response is received?


Solution

  • You don't provide the done callback to your test function:

    describe('bing.com', function() { it('should return 200 for a GET request', function(done) { ...

    To catch errors like that you should check your Code with JSHint (or Jslint). Both would inform you that your done() call won't work as the variable is not defined.