Mocha Exception — Timeout of 2000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves
Recently I tried to write a small Mocha test with Selenium web-driver.
I have installed required modules, and written a demo test with ‘describe’ and ‘it’ block.
Running the test using “mocha” in terminal, I get an exception as “Timeout of 2000ms…”. Screenshot provided below:
After browsing couple of pages, I found that each function(test) has a default timeout limit for 2 seconds (2000ms).
When our tests run longer than 2000ms, then the timeout reaches maximum limit and breaks the tests.
We have two options to fix this:
a) We can write our test which will complete in less than 2 seconds. (Typically unit tests validate the function and components with input/output values do not take longer than 2 seconds.)
b) Increase the timeout limit for the test for more than 2 seconds.
To increase timeout limit for test case, we can add the time in the describe block, which will be inherited by all child functions for this scenario (test suite).
2. To increase the timeout limit globally for all tests we can update out scripts in package.json file to set desired timeout limit as below:
With above two options we can fix this issue.