I was writing unit tests for my application. When working with tests, it ran as expected. But when I try to ran the test by modifying the objectID but as a valid one to check for not found condition. In this, the code inside the end block doesn't get execute
import { before, beforeEach, describe, test } from "mocha";
import { ExpenseList } from "../src/models/expenseList.model.js";
import { BASE_URL } from "../src/constants.js";
import { User } from "../src/models/users.model.js";
import { generateToken } from "../src/services/jwt.service.js";
import { server } from "../src/index.js";
import { chai, expect } from "./chai.js";
describe.only("Expense List Tests", () => {
const url = `${BASE_URL}/explists`;
let token;
let id;
before((done) => {
User.create({
email: "[email protected]",
password: "Test@123",
name: "Test User",
}).then((res) => {
id = res._id;
token = generateToken({ id, name: res.name });
done();
});
});
beforeEach((done) => {
ExpenseList.deleteMany({}).then((res) => {
done();
});
});
//7 passing test excluded for simplicity
test("Not found ID", done => {
ExpenseList.create({
title: "Test List",
createdBy: id
}).then(exp => {
const fakeId = exp.id.slice(0, 20) + "1234";
console.log(fakeId);
chai.request.execute(server)
.get(`${url}/${fakeId}`)
.set("Authorization", token)
.end((err, res) => {
try{
expect(res).to.have.status(404);
expect(res.body).to.be.an('object');
expect(res.body).to.have.property('errors');
expect(res.body.errors).to.be.an('object');
expect(res.body.errors).that.has.property('id');
expect(res.body.errors.id).to.be.a('string');
done();
}
catch(err){
done(err)
}
})
})
})
})
after((done) => {
User.deleteMany({}).then((res) => {
done();
});
});
});
The test ran out of time and throws the timeout error by saying
677d5e15b9d57d3843621234
1) Not found ID
7 passing (10s)
1 failing
1) Expense List Tests
GET /explist/:id
Not found ID:
Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/santhosh/Documents/Whizlab Tasks/Week 3/ExpenseTracker/expense-backend/test/expenseList.spec.js)
at listOnTimeout (node:internal/timers:594:17)
at process.processTimers (node:internal/timers:529:7)
If i provide correct id, it enters into the end block. But when the id is not found it comes with this error. I debugged the code and found that the code escapes the end block before it returns the response. I don't know why!