Reputation: 622
I'm just starting learning mocha and chai. And I get stuck here
const UserService = new Service();
describe("user-services.spec", () => {
describe("Services testing", () => {
before((done) => {
db();
userModel.remove({}, done);
})
after((done) => {
mongoose.connection.close();
done();
})
it("should add user to db", () => {
let id = 4;
let name = "Alen";
(async () => {
let result = await UserService.addUser(id, name);
console.log("result", result);
result.should.have.property("_i");
//done();
})();
})
})
})
Now I have two question based on above code
Is that this test case always pass even if I change "_id" to "_i" I don't know how ?
If I want to use done with above code and uncomment the done() then it gives error
Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Upvotes: 0
Views: 435
Reputation: 15177
For your first question, I've tested using result.should.have.property("_i");
and it fails.
I've mocked the method like this:
async function addUser(id, name){
return {_id:""}
}
And it throws
Uncaught AssertionError: expected { _id: '' } to have property '_i'
As expected.
So check the value returned. Also you can check chai documentation
And for the second question, done()
is the callback to say Mocha where the function is done. Also Mocha has a timeout (max. time the test will be waiting). If the maximum time is reached without a done()
calling, Mocha will thrown an error saying it has not been finished (done is not called).
If you don't call done()
Mocha will stuck waiting without knowing when the function is completed.
You get the error Timeout of 2000ms exceeded
because 2 seconds is the default value as documentation says.
Specifies the test case timeout, defaulting to two (2) seconds (2000 milliseconds). Tests taking longer than this amount of time will be marked as failed.
Upvotes: 1