1

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?

David Hellsing
  • 106,495
  • 44
  • 176
  • 212
mikewilliamson
  • 24,303
  • 17
  • 59
  • 90
  • 1
    Please consider negating the title; as it stands now, it is ultimately correct but confusing. – 11684 Dec 03 '13 at 13:30
  • 2
    It's not passing (or failing), it's not being run at all ("0 assertions"). See the [documentation](http://pivotal.github.io/jasmine/#section-Asynchronous_Support) on how to run asynchronous tests. – JJJ Dec 03 '13 at 13:31

1 Answers1

1

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();
      });
  });
Matthew Bakaitis
  • 11,600
  • 7
  • 43
  • 53
  • 2
    Also, just for the record, it turns out that response does not have a method "body". I switched to use the "request" library which seems to be much nicer to deal with: https://npmjs.org/package/request – mikewilliamson Dec 03 '13 at 14:27