How to deal with mocha timeout issues?

How to deal with mocha timeout issues?

Timeout error for integration tests is the number 1 issue I face with Mocha test runner and Sequelize ORM library.

JavaScript and consequently NodeJS does not have concurrency, that is, their processes do not run at the same time using multiple threads (this is changing though). Rather it runs on a single thread using an event loop machine that runs code asynchronously.

How do I solve it?

Better Organisation

I can try structuring my codes so that there is less statement to cover, thereby less process to run and less memory required. But as in most integration tests, we want to cover scenarios closest to real life. That is, instead of testing solely a function, we prefer covering a complete feature. An example is, creating a login for a user and sending him or her an email of confirmation.

These types of tests usually take plenty of resources as we need to create dummy objects, manipulate them and delete them from the test database. We certainly don't want to use real APIs as they may be costly such as sending real mails to a mailing service that charges per mail.

As a result, with all these processes, most testing procedures often results in timeouts.

Quick Fix

The best way and easiest way is to use timeouts.

In the upper most describe block, I add this.timeount(6000) or more, depending on the number of operation. This solves the problem, unless too much database access requests are required.

If you want to improve your NodeJS development skills in a few hours, I highly recommend this course: