georoot georoot - 3 days ago 6
Node.js Question

Supertest shows passing when it should fail

Here is the output from morgan


::ffff:127.0.0.1 - - [01/Dec/2016:23:44:19 +0000] "GET /temp HTTP/1.1" 404 17 "-" "node-superagent/2.3.0"


And the tests that i have written are

var chai = require("chai");
var request = require('supertest');
var server = require("../app");

describe("Basic tests", function() {
describe("Check if server is running", function() {
it("Root endpoint returns 200", function() {
request(server)
.get('/temp')
.expect(200)
.end();
});
});
});


The
morgan
is showing
404
code that i can also confirm by passing a function in
end()
but the test are running fine in mocha with no errors. Where am i going wrong ?

Answer

The problem is that the test is async and mocha assumes that, if you don't return a promise from your test or specify a callback function, your test is synchronous (more info here).

So, add a callback function.

it("Root endpoint returns 200", function(done) {
  request(server)
    .get('/temp')
    .expect(200)
    .end(done);
});
Comments