## Timeout ### Timeout is set per test case If set globally, the Mocha timeout starts to count per test case. When running the following code with `mocha test --timeout 5000`, test 1 fails, while test 2 succeeds. ```js describe('suite', function() { it('test 1', function(done) { setTimeout(done, 6000) }) it('test 2', function(done) { setTimeout(done, 4000) }) }) ``` ### When working with callbacks, hook execution time *does not* count towards the timeout The execution time of hooks is not taken into account when working with callbacks. So, when running the following code with `mocha test —timeout 5000`, only test 1 fails. ```js describe('suite', function() { beforeEach(function (done) { setTimeout(done, 4000) }) it('test 1', function(done) { setTimeout(done, 5000) }) it('test 2', function(done) { setTimeout(done, 4000) }) }) ``` ### When working with promises, hook execution time *does* count towards the timeout When using promises inside the tests, the hook execution time is counted towards the timeout. So, `mocha test --timeout 5000` already fails at the hook, because the hook execution time exceeds the timeout value. ```js function delay(delayMs) { return new Promise((resolve) => setTimeout(resolve, delayMs)) } describe('suite', function() { beforeEach(async function () { await delay(6000); }) it('test 1', async function() { await delay(5000); }) it('test 2', async function() { await delay(4000); }) }) ``` If needed, the timeout for the hook can be set individually by calling e.g. `this.timeout(8000)` in the hook. This would also of course work for overriding the general timeout in the `it` blocks. ## Typescript The easiest setup procedure I found here: https://medium.com/@ufukbakan/testing-react-with-mocha-and-typescript-ultimate-guide-54332de7cf36 - ts-node - Type definitions for node and mocha - tsconfig - Bit of mocha config ## Order of executed hooks Given this test suite (and root-level hooks configured): ```js describe("dummy test", function () { before(async function () { // }); beforeEach(async function () { // }); afterEach(async function () { // }); after(async function () { // }); it("runs", async function () { assert(true); }); }); ``` … then Mocha runs the hooks in the following order: ``` Root-level before before Root-level beforeEach beforeEach (test case) afterEach Root-level afterEach after Root-level after ```