Search code examples
node.jstypescriptchaichai-http

No response when try test nodejs express4 route with chai-http


I try run test for nodejs express4 routes and can't get response, if try run app and perform native (POstMan) test - work like a sharm).

APP.ts

import express = require('express');
import expressValidator = require('express-validator');
import bodyParser = require('body-parser');
let app = express();

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(expressValidator())

app.get("/", (req, res) => res.status(200).send({message: "Hello"}));

app.route("/book")
    .get((req,res)=>{console.log(req.path);return res.status(200)})
    .post((req,res)=>{return res.status(

//Run
app.listen(3000, function() {
  console.log('Listening on port '+ 3000)
})

export = app;

test.ts

import chai = require('chai');
import chaiHttp = require('chai-http');
import mocha = require('mocha');

let server = require('../dist/app');
let expect = chai.expect;

chai.use(chaiHttp);

describe('/GET', () => {
      it('it should not GET a book without pages field', (done) => {
                        chai.request(server)
                        .get('/book')
                        .end(function(err, res) {
                            expect(res).to.have.status(200);
                            done();                               // <= Call done to signal callback end
                        });
            });
  });

As result I get error

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.


Solution

  • /book doesn't send any response back to client. That's why your testcase is timing out.

    res.status(200) only set the http response code to 200, but doesn't send any response. You can try resp.sendStatus(200).