0

Hello I just try to do some functional test with mocha + chai on nodeJS api and everytime I try something even if it's not what expected from test, the test seems to pass.

Here is my test code :

const server = require("../server");
const chai = require("chai");
const chaiHttp = require("chai-http");

chai.use(chaiHttp);

const expect = require("chai").expect;
const requester = chai.request(server);

describe("test user", function(){
    describe("get all", function(){
        it("should return all users and 400", function(){
            requester.get("/api/users")
            .then(function(res){
                console.log(res.status);
                expect(res).to.have.status(400);
            })
            .catch(function(err){
                throw(err);
            })
        })
    })
})

and this is what I get :

> backend@1.0.0 test
> mocha "tests/users.test.js"

App listening at http://:::4000


  test user
    get all
      ✔ should return all users and 400


  1 passing (13ms)

Successfully connected to MongoDB.
200

since I wait 400 and get 200 it should fail but it isn't.

LeMecWeird
  • 35
  • 6

1 Answers1

0

The problem is that you are testing a promise so your test is finished before the promise resolves (with either success or error). In order to make mocha wait for the promise you can do one of the following:

  1. Return the promise from the mocha it block
describe("test user", function(){
    describe("get all", function(){
        it("should return all users and 400", function(){
            return requester.get("/api/users")
            .catch(function(res){
                expect(res).to.have.status(400);
            })
        })
    })
})
  1. use async function and await the promise, then perform the expecations:
describe("test user", function(){
    describe("get all", function(){
        it("should return all users and 400", async function(){
            const res = await requester.get("/api/users")
            expect(res).to.have.status(400);
        })
    })
})

Here you can omit the catch clause since mocha will figure it out.

You can read more about testing async stuff in the official mocha docs.

fgkolf
  • 910
  • 3
  • 15
  • [this is what i get with both solution](https://pastebin.com/gGPg4aHy) – LeMecWeird Oct 28 '21 at 14:34
  • Does your promise resolves? If you put a `console.log(res)` after the `await` line in example 2, do you see your response logged? – fgkolf Oct 28 '21 at 14:38
  • Yes my response is printed but after mocha prompted the error, but yes i get my response – LeMecWeird Oct 28 '21 at 14:42
  • So is there any chance your api is pretty slow and responds in more than `2000ms`? You can try something [like this](https://stackoverflow.com/a/16607408/13168083) to increase mocha timeout. – fgkolf Oct 28 '21 at 14:47
  • Men you save my life, my api make 3.5 sec to send me response so it was the timeout ! Thanks that working – LeMecWeird Oct 28 '21 at 14:49
  • Glad it did @LeMecWeird. Keep in mind that you still need to apply the code from my answer to properly handle the async part. Also feel free to accept the answer and vote it up if worked for you! – fgkolf Oct 28 '21 at 14:55