Node-Jasmine not failing when expected

389 views Asked by At

I'm trying to setup a node-jasmine test for the first time. Currently I'm just trying to setup a simple test to see that getting the index returns status 200.

It seemed to be working but I noticed no matter what I change the status number to it never fails, for example expecting status 666, but I don't get a failure:

const request = require("request")
const helloWorld = require("../app.js")
const base_url = "http://localhost:3002/"

describe("Return the index page", function() {
  describe("GET /", function() {
    it("returns status code 200", function() {
      request.get(base_url, function(error, response, body) {
        expect(response.statusCode).toBe(666)
        done()
      })  
    })  
  })  
})

Which returns:

Finished in 0.009 seconds
1 test, 0 assertions, 0 failures, 0 skipped

When I expected a failure here.

2

There are 2 answers

0
duncanhall On BEST ANSWER

You need to include the done callback as a parameter to the test function.

Eg:

it("returns status code 200", function(done) {
  request.get(base_url, function(error, response, body) {
    expect(response.statusCode).toBe(666)
    done();
  })  
}) 

Without this, the test is completing before the asynchronous request returns.

0
A. Weatherwax On

While it looks like you found your answer, I came here with a similar problem. My problem was that the request was failing, and the assertion was never reached. Once I added an error catch like below, I found my problem!

I'm new to Jasmine, but it seems odd that an exception generated inside your test wouldn't be more visible or apparent, so if anyone has feedback on how to better handle, let me know.

const request = require("request")
const helloWorld = require("../app.js")
const base_url = "http://localhost:3002/"

describe("Return the index page", function() {
  describe("GET /", function() {
    it("returns status code 200", function() {
      request.get(base_url, function(error, response, body) {
        if (error)
          console.log("Something borked: ", error);

        expect(response.statusCode).toBe(666)
        done()
      })  
    })  
  })  
})