paulbjensen
paulbjensen

Reputation: 838

nested expect() function with Jasmine BDD does not evaluate

I have the following Jasmine context and assertion:

it "should return a javascript file", ->
  # We make a request to /x.js
  request {uri: 'http://localhost:3000/x.js'}, (err, res, body) ->
    expect(res.statusCode).toEqual 200

When I call Jasmine to evaluate the spec, it does not pickup the assertion. How can I make it pickup the assertion?

Upvotes: 0

Views: 968

Answers (1)

Alex Wayne
Alex Wayne

Reputation: 187074

Your it() is exiting before your request returns a response. With jasmine, you have to think about asynchronous events a bit harder.

Check out waitsFor() and do something like this, which will prevent your spec from exiting until the callback is executed, or the default timeout period has elapsed.

it "should return a javascript file", ->
  responded = no
  request {uri: 'http://localhost:3000/x.js'}, (err, res, body) ->
    responded = yes
    expect(res.statusCode).toEqual 200

  waitsFor -> responded

This will also cause your spec to fail if the server times out, because each waitsFor() expects to be satisfied eventually.

Upvotes: 1

Related Questions