1

I am testing a MEAN app. Each test is just making sure I'm getting the right response codes back. In my routers, they are successfully returning json using res.send().

The second test given here will only pass if the first one is commented out. If I comment out the first test, the first one passes, but the second one times out.

This behavior isn't unique to these two tests. Before 'it accepts lat and long', there is another test. If I comment that out, accepts lat and long works. If I leave it in, accepts lat and long times out. What do I need to do to get these async tests to pass?

I have tried setting the timeout to something like 60 seconds, but that doesn't work either.

var assert = require('assert');
var server = require('../bin/www');
var request = require('supertest');

request = request('http://localhost:3000');

describe('POST service request', function(){
    this.timeout(5000);
    var postRequest = request.post('/requests.json').type('form');

... (other commented out tests) ...

// it('accepts lat and long', function (done){
//  postRequest.send({
//      service_code: 2000,
//      long: 400,
//      lat: 3003
//  }).expect(200, done);
// });


    it('accepts an address id only', function (done){
    console.log('22222')
        postRequest.send({
            service_code: 100,
            address_id: 400
        }).expect(200, done);
    });
});

Here is some logged output when they are both uncommented:

Listening on port 3000
  POST service request

===REQUEST STARTED===

Trying to save a request.
DB connected.

Saved.

POST /requests.json 200 40.368 ms - 49

    ✓ accepts lat and long (47ms)

22222
1) accepts an address id only


  1 passing (5s)
  1 failing

  1) POST service request accepts an address id only:
     Error: timeout of 5000ms exceeded
      at null.<anonymous> (/usr/lib/node_modules/mocha/lib/runnable.js:159:19)
      at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)


npm ERR! Test failed.  See above for more details.

npm ERR! not ok code 0

And then it times out.

chris
  • 1,831
  • 18
  • 33

1 Answers1

1

You can't re-use the same postRequest between tests, since sending the request has side-effects. Create a new request in each test:

it('accepts lat and long', function (done){
    request.post('/requests.json').type('form').send({
        service_code: 2000,
        long: 400,
        lat: 3003
    }).expect(200, done);
});

it('accepts an address id only', function (done){
    request.post('/requests.json').type('form').send({
        service_code: 100,
        address_id: 400
    }).expect(200, done);
});
Paul
  • 139,544
  • 27
  • 275
  • 264