简体   繁体   English

Mocha.js:如何在所有测试完成后运行一些代码?

[英]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.一种典型的试验MochaJS,当然,包括beforeafter ,人们可以运行一个安装/拆卸块。

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?我怎么能在 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.最新的 Mocha 版本 (8.x) 支持 root hook 插件,如果您在串行模式下运行测试,它可以完全满足您的需要。 There is a good point to start: https://mochajs.org/#root-hook-plugins有一个很好的起点: https : //mochajs.org/#root-hook-plugins

You can run Mocha programmatically .您可以以编程方式运行Mocha The nice thing is that Mocha support events for the runner that exposes exactly what you need.好消息是 Mocha 支持运行器的事件,它可以准确地公开您需要的内容。

mocha.run() returns a Runner instance which emits many events of interest. mocha.run() 返回一个 Runner 实例,它发出许多感兴趣的事件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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