如何修复“超时不正常!”测试固定路线时?

时间:2019-03-30 17:29:32

标签: node.js testing tap fastify

我正在尝试使用tap测试固定路线。这是测试文件:

const tap = require('tap')
const buildFastify = require('../../src/app')

tap.test('GET `/` route', t => {
    t.plan(4)

    const fastify = buildFastify()

    // At the end of your tests it is highly recommended to call `.close()`
    // to ensure that all connections to external services get closed.
    t.tearDown(() => {
        fastify.close();
    });

    fastify.inject({
        method: 'GET',
        url: '/'
    }, async (err, response) => {
        t.error(err)
        t.strictEqual(response.statusCode, 200)
        t.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
        t.deepEqual(JSON.parse(response.payload), { hello: 'world' })
        t.done()
    })
})

运行测试后,我会在控制台中看到:

....Closing mongoose connection ...
listening on 3001
tests/routes/status.test.js ........................... 4/5 30s
  not ok timeout!

使用npm脚本运行测试: "test": "env-cmd ./test.env tap tests/routes/status.test.js"

以下是带有app.js功能的buildFastifybuildFastify on gist

1 个答案:

答案 0 :(得分:2)

嗯...我今天遇到了同样的问题。我试图编写一个用于猫鼬连接的插件,但遇到了点击超时的问题。

解决方案是在“ onClose”钩子上关闭数据库连接,Fastify默认情况下不执行此操作。

我看到您的代码中有“ onClose”钩子,对此感到很惊讶。

std::vector<std::shared_ptr<Layer>> _layers;