使用 express 完成测试运行后,Jest 一秒未退出 [英] Jest did not exit one second after the test run has completed using express

查看:25
本文介绍了使用 express 完成测试运行后,Jest 一秒未退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JEST 对我的快速路由进行单元测试.

I'm using JEST for unit testing my express routes.

在运行 yarn test 时,我的所有测试用例都通过了,但出现错误

While running the yarn test all my test case are getting passed, but I'm getting an error

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,但仍然抛出上述错误.

I used async & done, but still it throws the above error.

下面是我的规范代码.请帮忙

Below is my spec code. Please help

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();
  });
});

推荐答案

我的问题是通过这段代码解决的:

My problem was solved by this code:

beforeAll(done => {
  done()
})

afterAll(done => {
  // Closing the DB connection allows Jest to exit successfully.
  mongoose.connection.close()
  done()
})

这篇关于使用 express 完成测试运行后,Jest 一秒未退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆