Jest 在所有测试之前运行一次异步函数 [英] Jest run async function ONCE before all tests

查看:29
本文介绍了Jest 在所有测试之前运行一次异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use jest for my server unit testing (instead of mocha+chai). Is there a way I can run async function before all tests start (init purposes) only once and not for every test file? And also if there's a way of running something after all tests are done?

推荐答案

此功能已添加到 Jest 的 22 版本,带有 globalSetupglobalTeardown 配置.查看 this 以获取示例.

This feature was added in Jest's 22 version, with globalSetup and globalTeardown configurations. Look at this for examples.

package.json(或在 jest.config.js)

{
  ...
  "jest": {
    "globalSetup": "./scripts/jestGlobalSetup.js"
  }
}

/scripts/jestGlobalSetup.js

module.exports = async () => {
  console.log('
hello, this is just before tests start running');
};

export default async () => {
  console.log('
hello, this is just before tests start running');
};

这篇关于Jest 在所有测试之前运行一次异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆