1

I'm writing Jasmine tests for an app I'm building with Node.js and Express. One of the first tests is to see if the app responds with a statusCode of 200. I was working through this tutorial which shows you how to do just that, but I've hit a snag.

jasmine-node won't run the tests. There aren't any failures; it just doesn't give a report. Apparently, this is a known bug.

Looking at the jasmine-node project, though, it hasn't been updated in over a year! Looking over at the main jasmine project, I see that it now has support for Node! So, has jasmine-node been abandoned in favor of adding support for Node into jasmine? After installing jasmine and running it against the specs, I now have a new problem. When running either jasmine or npm test, instead of getting Jasmine test failures, I get

Started

TypeError: Cannot read property 'statusCode' of undefined

Here is my spec file:

var request = require("request");
var base_url = "http://localhost:3000/";

describe("Hello World Server", function() {
  describe("GET /", function() {
    it("returns status code 200", function(done) {
      request.get(base_url, function(error, response, body) {
        expect(response.statusCode).toBe(200);
        done();
      });
    });

    it("returns Hello World", function(done) {
      request.get(base_url, function(error, response, body) {
        expect(body).toBe("Hello World");
        done();
      });
    });
  });
});

1 Answers1

0

Looks like you're not getting a valid response. You should check out the error in your callback function:

request.get(base_url, function(error, response, body) {

    // Check for error
    if(error){
        console.log(error);
        // Probably assert a failure here.
    }

    expect(response.statusCode).toBe(200);
    done();
});

After inspecting the error using console.log, you may not want to remove the logging and replace it with some jasmine logic for error checking.

Tyler
  • 17,669
  • 10
  • 51
  • 89