Fastify测试单元不断超时(节点点击)



我正在Fastify上使用Node Tap测试NodeJS应用程序。我对这两个平台都是新手。测试用例单元应该连接到graphql并运行一个示例查询。我可以看到查询被执行,测试断言被成功通过:

Executing (default): SELECT `id`, `title`, `type`, `description`, `published`,` FROM `articles` AS `Article` WHERE `Article`.`section_id` IS NOT NULL AND `Article`.`date_published` <= NOW() AND ORDER BY `Article`.`TITLE` ASC,LIMIT 5;

然而,测试用例持续花费30秒,最终超时。I

FAIL  test/fastify-tests.js
✖ timeout!
test: TAP
signal: SIGTERM
handles:
- type: Socket
events:
- end
- data
- error
- clientError
- close
- drain
expired: TAP

我还注意到它在测试数量上增加了一个额外的断言。例如,在我只有一个断言的情况下,它声明它正在测试两个:

Asserts:  1 failed, 1 passed, of 2

我的测试用例:

const tap = require('tap');
const fastify = require('../fastifyapp');
tap.test('POST  to `/graphql` route', t => {
t.plan(1); //plan for 1 assertion
const app = fastify.FastifyApp();
app.inject({
method: 'POST',
url: 'http://localhost/graphql',
payload: {
"operationName:": null,
"query": '{n' +
'article(limit: 5) {n' +
'idn' +
'}n' +
'}n',
"variables": {
"visibility": "PUBLIC",
"articletype": "SPORTS",
"OFFSET": 0,
"articleSortBy": "ID",
"articleSort": "ASC",
}
}
}, (err, response) => {
t.error(err);  // assertion 1
});
});

如果有人能帮我理解原因,我会很高兴的:

  • 每次都超时
  • 它在正在进行的测试列表中添加了一个额外的断言

t.error()断言之后添加t.end()

您需要关闭与数据库的连接。


1.通过使用快速挂钩:

我有一个关于mongoose的例子,你可以搜索postgresql、mysql或你连接到的任何数据库:

fastify
.decorate('mongoose', db)
.addHook('onClose', (fastify, done) => {
fastify.mongoose.connection.close();
});

2.通过分离数据库连接:

您可以提供数据库实例作为选项。并定义.close((回调函数,以便在每次进行测试时调用。

最新更新