我在用玩笑来测试我的特快路线。
在运行yarn test时,我的所有测试用例都通过了,但是我得到了一个错误
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.我使用了async & done,但仍然抛出了上面的错误。
下面是我的规范代码。请帮帮忙
routes.spec.ts
const request = require('supertest');
describe('Test the root path', () => {
const app = require('./index');
test('GET /gql/gql-communication-portal/release-notes', async (done) => {
const response = await request(app).get('/gql/gql-communication-portal/release-notes');
expect(response.status).toBe(200);
done();
});
});发布于 2019-11-07 11:11:35
这个代码解决了我的问题:
beforeAll(done => {
done()
})
afterAll(done => {
// Closing the DB connection allows Jest to exit successfully.
mongoose.connection.close()
done()
})发布于 2019-03-05 22:05:03
我也有同样的问题,但在我的package.json文件中,我添加了"test": "jest --detectOpenHandles"并运行了npm test --detectOpenHandles。这次我没有收到错误消息。也许你可以试试。
发布于 2020-08-07 09:31:52
在我这一边,我只是把app.listen()和我的应用程序分开。因此,使用express,您的应用程序以导出结束。
// index.js
module.exports = app;然后创建另一个文件来监听端口。
// server.js
const app = require('./index')
app.listen(...)而且,如果您只在测试中导入索引(app index.js),那么它不需要额外的配置就可以工作。当然,您需要调整您的快速应用程序的启动。它现在应该使用server.js。
https://stackoverflow.com/questions/53935108
复制相似问题