在 VSTS 中,连接到 dockerized ArangoDB 数据库的异步 Jest 测试超时

Posted

技术标签:

【中文标题】在 VSTS 中,连接到 dockerized ArangoDB 数据库的异步 Jest 测试超时【英文标题】:In VSTS, async Jest tests that connect to dockerized ArangoDB database time out 【发布时间】:2018-12-23 06:47:39 【问题描述】:

我正在尝试使用 Visual Studio Team Services 构建运行笑话测试。这些测试运行良好并在本地通过,但是当我在 VSTS 中运行它们时会超时。对于连接到数据库的每个异步测试,我得到 ​​p>

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

这是我的设置:

graphql API 使用 Apollo Server 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 数据库对象并获取数据库信息。

我尝试过的其他事情:

不会命中数据库的 Promise 测试,比如

it('foo', async () => 等待 Promise.resolve() 期望(1).toEqual(1) ) 这些通过

将超时时间增加到 30000。这会导致一些使用数据库调用的测试返回 null

【问题讨论】:

【参考方案1】:

我能够解决这个问题,我认为有两个问题:

    API 实际上没有连接到数据库。我可以通过创建一个新的 docker 网络并附加数据库和 VSTS 构建代理来解决这个问题,as described in this other answer 测试在数据库完全启动之前开始。我在似乎可以解决此问题的测试之前在 bash 脚本中添加了 sleep 命令。

【讨论】:

既然你的问题已经解决了,就可以标记答案了。

以上是关于在 VSTS 中,连接到 dockerized ArangoDB 数据库的异步 Jest 测试超时的主要内容,如果未能解决你的问题,请参考以下文章