javascript - Jest 在所有测试之前运行一次异步函数

标签 javascript jestjs

我想使用 jest 进行服务器单元测试(而不是 mocha+chai)。
有没有一种方法可以在所有测试开始(初始化目的)之前运行异步函数一次而不是每个测试文件?而且,如果在所有测试完成后有办法运行某些东西?

最佳答案

此功能是在 Jest's 22 version 中添加的, 与 globalSetupglobalTeardown配置。
this举些例子。
package.json (或在 jest.config.js 中)

{
  ...
  "jest": {
    "globalSetup": "./scripts/jestGlobalSetup.js"
  }
}
/scripts/jestGlobalSetup.js
module.exports = async () => {
  console.log('\nhello, this is just before tests start running');
};
或者
export default async () => {
  console.log('\nhello, this is just before tests start running');
};

关于javascript - Jest 在所有测试之前运行一次异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45315679/

相关文章:

javascript - rails : Disable form field when checkbox is checked

javascript - phonegap 4.0 媒体插件 ReferenceError : Media is not defined

javascript - 使用 nodejs 进行测试时,svgElement.getBoundingClientRect() 始终返回零

reactjs - 如何在 Jest 中为函数编写测试用例?

javascript - 跳过测试文件 Jest 中的一项测试

javascript - 使用 fireEvent 传递自定义事件属性(测试库和 Jest )

javascript - 调整浏览器窗口大小时保留在内容中

javascript - 如果用户在 iOS 设备上,隐藏 HTML 元素?

javascript - 使用 JavaScript Onclick 事件将数据传递到 Django 中的views.py?

testing - 如何对 native 应用程序进行自动设备测试?