QUnit.hooks

添加版本:2.18.0.

描述

QUnit.hooks.beforeEach( callback )
QUnit.hooks.afterEach( callback )

注册一个全局回调函数,在每个测试之前或之后运行。

参数 描述
callback (function) 要执行的回调函数。使用一个 assert 参数调用。

这相当于将一个 QUnit.module() 钩子应用于所有模块和所有测试,包括不与任何模块关联的全局测试。

与模块钩子类似,全局钩子支持异步函数或返回 Promise,QUnit 将在继续执行测试之前等待这些函数或 Promise。每个全局钩子还可以访问与运行钩子的 QUnit.test 相同的 assert 对象和测试上下文。

有关钩子的更多详细信息,请参阅 QUnit.module § 钩子

示例

QUnit.hooks.beforeEach(function () {
  this.app = new MyApp();
});

QUnit.hooks.afterEach(async function (assert) {
  assert.deepEqual([], await this.app.getErrors(), 'MyApp errors');

  MyApp.reset();
});