2

The following test is not working with mocha-chai, it is able to to get the input request but throws the error message.

 it('/hb  : ', function (done) {
            return chai.request(app)
                .post('/hb')
                .send({"a":1 })
                .then(function (res) {
                    expect(err).to.be.null;
                    expect(res).to.have.status(200);
                    // { ah: { rt: [Object] }, ad: { mojo: 1 } } }
                    //console.log("CAlling DOne ........... +");
                    done();
                }, function (err) {
                    //console.log(err);
                    throw err;
                });
});

Output:

Web Requests : /hb : : Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

halfer
  • 19,824
  • 17
  • 99
  • 186
shrw
  • 1,719
  • 5
  • 27
  • 50

2 Answers2

2

The functions that chai-http adds to chai return promises. In your code you return the promise, which is good. However, you also declare your test to take the a parameter: function (done). This would be fine if you did not return the promise, but returning the promise is really the better mechanism here. When you declare your test to take a parameter, Mocha ignores the return value from the test, and so the promise is ignored. So just remove your use of done.

Here's an example that reproduces the error you had in your original code with err being undefined in the function you pass to then.

'use strict';
var app = require('./server');
var chai = require('chai');
chai.use(require('chai-http'));

var expect = chai.expect;

it('/hb', function () {
    return chai.request(app)
        .post('/hb')
        .send({a: 1})
        .then(function (res) {
            expect(err).to.be.null;
            expect(res).to.have.status(200);
        });
});

If the server returns a 200 status, then you'll get this on the console:

  1) /hb

  0 passing (26ms)
  1 failing

  1)  /hb:
     ReferenceError: err is not defined
      at test.js:13:20

If the server returns a 400 status, the output would be:

  1) /hb

  0 passing (24ms)
  1 failing

  1)  /hb:
     Error: Bad Request
      at Test.Request.callback (node_modules/superagent/lib/node/index.js:792:17)
      at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/index.js:990:12)
      at endReadableNT (_stream_readable.js:913:12)
Louis
  • 146,715
  • 28
  • 274
  • 320
  • But why errors breaking the mocha with message : npm ERR! Linux 3.10.0-514.10.2.el7.x86_64 npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "test" npm ERR! node v6.10.0 npm ERR! npm v3.10.10 npm ERR! code ELIFECYCLE npm ERR! bidonhomesnode@1.0.0 test: `mocha ./tests/` npm ERR! Exit status 1 – Ritesh Mar 30 '17 at 09:34
  • That's a different problem then the one reported by the OP. If you cannot find an answer to it, you can post a new question. Make sure to include a [mcve]. – Louis Mar 30 '17 at 10:15
0

you need to add following:

.set('content-type', 'application/x-www-form-urlencoded')

you can reference this question over Post request via Chai

Archer11
  • 75
  • 3
  • 1
    First, this is not the issue at all. The problem is that the OP is unable to get Mocha to detect that the test failed. Setting the content type is not going to fix that. Second, if really this were the answer to this question, then what you do is flag this question as a duplicate of the other one. What you do *not* do is post an answer that duplicates an answer from somewhere else. – Louis Aug 11 '16 at 19:16