onkami
onkami

Reputation: 9411

Mocha.js : how to run some code after all tests are completed?

A typical MochaJS test, of course, includes before and after blocks where one can run a setup/teardown.

However, I would like to perform an additional teardown step after all test files are processed - to take into account any loose resources that individual teardowns may forget to delete.

How could I achieve that in MochaJS?

Upvotes: 3

Views: 1778

Answers (2)

Alex Povar
Alex Povar

Reputation: 4960

Most recent Mocha's version (8.x) bring support for root hook plugin, which allow to do exactly what you need, if you running tests in serial mode. There is a good point to start: https://mochajs.org/#root-hook-plugins

Upvotes: 1

Daniel
Daniel

Reputation: 2531

You can run Mocha programmatically. The nice thing is that Mocha support events for the runner that exposes exactly what you need.

mocha.run() returns a Runner instance which emits many events of interest.

 * Events:
 *   - `start`  execution started
 *   - `end`  execution complete
 ...

Upvotes: 0

Related Questions