javascript - Mocha Requires me to read HTTP Request Body -


i'm attempting use mocha write tests node.js api i'm working on. current tests i'm writing interested see if responses coming correct status code - not care response body.

as such, have written following test:

note, isn't real test. i've cut out project-specific stuff. error does still occur on code below.

var http = require('http'); var assert = require("assert");  before(function() {     var server = http.createserver(function(req, res) {         res.end();     }).listen(8080); });  describe("convenience functions", function() {      it("should return 200 status code", function(done) {         http.get("http://localhost:8080", function(res) {              assert.equal(res.statuscode, 200);              res.on('end', function() {                  done();             });         });     }); }); 

running test, however, gives me timeout mocha. mocha defaults 2000ms timeout, change unnecessarily high number , still timeout.

i've been fighting hours, , have found "fix". if change test to:

describe("convenience functions", function() {      it("should return 200 status code", function(done) {         http.get("http://localhost:8080", function(res) {              assert.equal(res.statuscode, 200);              res.on('data', function() { })              res.on('end', function() {                  done();             });         });     }); }); 

the test no longer times out. notice only difference between these 2 tests second version handling data event on response. doesn't data, defines listener on it. test passes flying colors.

this easy enough fix make, i'm confused why need this. shouldn't mocha tests finish call done()?

   http.get("http://localhost:8080", function(res) {       assert.equal(res.statuscode, 200);       done();    }); 

that's need. data/end events useful if response had body, doesn't , in case don't care, call done() , ignore response other checking it's status code.

shouldn't mocha tests finish call done()?

yes, , do. problem in first snippet end event never fires don't ever call done().


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -