With its default “BDD”-style interface, Mocha provides the hooks before(), after(), beforeEach(), and afterEach().
These should be used to set up preconditions and clean up after your tests.
Describing Hooks
Any hook can be invoked with an optional description, making it easier to pinpoint errors in your tests.
If a hook is given a named function, that name will be used if no description is supplied.
Asynchronous Hooks
All hooks (before(), after(), beforeEach(), afterEach()) may be sync or async as well, behaving much like a regular test-case.
For example, you may wish to populate database with dummy content before each test:
Root-Level Hooks
A hook defined at the top scope of a test file (outside of a suite) is a root hook.
As of v8.0.0, Root Hook Plugins are the preferred mechanism for setting root hooks.
Delayed Root Suite
If you need to perform asynchronous operations before any of your suites are run (e.g. for dynamically generating tests), you may delay the root suite.
Run mocha with the --delay flag.
This will attach a special callback function, run(), to the global context: