简体   繁体   中英

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?

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

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
 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM