gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:40:54 25 4
gpt4 key购买 nike

我想使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com