Why isn’t this jasmine-node test failing?

812 views Asked by At

I am using Express.js and Jasmine-node for the tests. This my server code:

var express = require('express');
var app = express();

app.get('/', function (request, response) {
  response.send("Bonjour!");
});

module.exports = app;

and this is the test:

var request = require('http');
var app = require('../src/app');
var server;

describe('textreader', function(){

  beforeEach(function(done){
    server = app.listen(3333, function (err, result) {
      if (err) {
        done(err);
      } else {
        done();
      }
    });
  });

  afterEach(function(){
    server.close();
  });

  it('responds to /', function(){
      request.get("http://localhost:3333", function(response){
        console.log(response.body);
        expect(response.body).toEqual('foo');
      });
  });

});

This is the output:

mike@sleepycat:~/projects/textreader☺  grunt
Running "jshint:files" (jshint) task
>> 3 files lint free.

Running "jasmine_node" task

textreader
    responds to /

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

Done, without errors.

So obviously this test should fail since the server can only produce the text "Bonjour!". Secondly, console.log produces no output at all.

What do I need to do to get this test failing as expected?

1

There are 1 answers

1
Matthew Bakaitis On BEST ANSWER

Your example is close to what is found on the jasmine-node page describing async tests.

You need to add done() to let the test to know to wait 500ms (by default) for the transaction to complete (or display a timeout error instead). Your code would look something like this:

it('responds to /', function(){
      request.get("http://localhost:3333", function(response){
        console.log(response.body);
        expect(response.body).toEqual('foo');
        done();
      });
  });