Recently I started creating an Appium framework with Java Script to test an iOS application. I have installed Mocha by using npm install --save-dev mocha
and I wrote a simple test that will just open the app and click on one button:
As you can see from the code above, the test is written with ‘describe’, ‘before’, ‘after’, and ‘it’ blocks, which are provided when you are using Mocha.
I ran the test with the npx mocha test.js
command in the terminal, but unfortunately, I’ve got the following exception:
Timeout of 2000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves.
I have investigated the error and found out that each function has a default timeout limit of 2 seconds or 2000 ms.
When the tests run longer than 2 seconds, the timeout reaches the maximum limit and the exception from above is shown in the terminal. Automatically, the test fails.
How to fix it?
I have managed to fix it by increasing the timeout to more than 2 seconds. To do that, add the timeout time when you are running the test with Mocha. You can do it like this:
npx mocha --timeout=30000 test.js
You can also include the command in the package.json file. For example, if all your tests are in the specs folder, you can add this in the package.json file:
"scripts": {
"test": "mocha --timeout 30000 specs/**/*.js"
}
The next time when you run the test with the npm test you will run all the tests from the specs folder without worrying about the timeout.
I hope that this will also fix your problem.