我想使用 jest 进行服务器单元测试(而不是 mocha+chai)。
有没有一种方法可以在所有测试开始(初始化目的)之前运行异步函数一次而不是每个测试文件?而且,如果在所有测试完成后有办法运行某些东西?
最佳答案
此功能是在 Jest's 22 version 中添加的, 与 globalSetup
和 globalTeardown
配置。
看this举些例子。
package.json (或在 jest.config.js 中)
{
...
"jest": {
"globalSetup": "./scripts/jestGlobalSetup.js"
}
}
/scripts/jestGlobalSetup.jsmodule.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/