我正在尝试使用Visual Studio Team Services构建运行jest测试 . 这些测试运行正常并在本地传递,但是当我在VSTS中运行它们时超时 . 对于连接到数据库的每个异步测试,我得到了
Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
这是我的设置:
使用Apollo Server的
-
graphql API
-
ArangoDB数据库内部有一个docker容器
典型的测试如下:
const database = require('../models')
...
describe('database setup', () => {
it('sets up the database and it exists', () => {
console.log(database.db)
const collection=database.db.collection('agents')
console.log(collection)
return database.db.exists().then((result) => {
expect(result).toBeTruthy()
}).catch(err => {console.log(err)})
.then(x => console.log(x))
})
}
...
describe('help functions', () => {
it('gets edge count for a node', async () => {
let result = await database.getEdgeCount('nodes/1', 'inbound')
expect(result).toBeGreaterThan(2)
})
})
我正在使用NPM任务在VSTS中运行测试 . 此任务的YAML是基本的:
steps:
- task: Npm@1
displayName: npm test
inputs:
command: custom
workingDir: api
verbose: false
customCommand: 'test --runInBand'
我知道测试正在连接到数据库,因为我可以 console.log
数据库对象并获取数据库信息 .
我试过的其他事情:
- 承诺没有命中数据库的测试,例如
它('foo',async()=> {等待Promise.resolve()expect(1).toEqual(1)})这些传递
- 将超时增加到30000.这会导致一些带有数据库调用的测试返回
null
.
1 回答
我能解决这个问题,我认为还有两个问题:
API实际上并未连接到数据库 . 我能够通过创建一个新的docker网络并附加数据库和VSTS构建代理来解决这个问题,as described in this other answer
测试在数据库完全启动之前开始 . 我在测试之前在bash脚本中添加了一个
sleep
命令,似乎解决了这个问题 .