在学 fullstack课程 使用了 Atlas 的 MangoDB 作为后端数据库,那么每次测试的时候都需要去连接数据库并使用 beforeEach
函数初始化数据库,由于网络访问的问题经常会使 jest 测试出现 Timeout 的现象不能完成测试:
Timeout - Async callback was not invoked within the 1000 ms timeout specified by jest.setTimeout.Error: Timeout - Async callback was not invoked within the 1000 ms timeout specified by jest.setTimeout.
所以为了能在内网顺利完成测试(即使会很慢)需要调整 jest 的 Timeout。
创建 jest 配置文件
在项目根目录创建 jest.connfig.js
并写入:
module.exports = {
// 由于是后端的测试所以选择node
testEnvironment: 'node',
setupFilesAfterEnv: ['./jest.setup.js'],
}
创建 jest 环境初始化后的设置
在项目主目录创建 jest.setup.js
并写入:
jest.setTimeout(30000)
这将使 jest 运行测试时等待异步请求的超时小于30s以内,比默认设置的 1秒多了很多余地。