首页 > 解决方案 > Jest 检测到以下 1 个打开的句柄可能会阻止 Jest 退出

问题描述

这是我的 HTTP 路由

 app.get('/', (req, res) => {
    res.status(200).send('Hello World!')
})

app.post('/sample', (req, res) => {
    res.status(200).json({
        x:1,y:2
    });
})

我想测试以下

1)GET要求工作正常。

2) /sample响应包含属性xy

const request = require('supertest');
const app = require('../app');

describe('Test the root path', () => {
    test('It should response the GET method', () => {
        return request(app).get('/').expect(200);
    });
})

describe('Test the post path', () => {
    test('It should response the POST method', (done) => {
        return request(app).post('/sample').expect(200).end(err,data=>{
            expect(data.body.x).toEqual('1');

        });
    });
})

但是在运行测试时出现以下错误

Jest 检测到以下 1 个可能阻止 Jest 退出的打开句柄:

返回请求(app).get('/').expect(200);

标签: javascriptexpresstestingjestjs

解决方案


你需要done()调用end()方法

const request = require("supertest");
const app = require("../app");

let server = request(app);

it("should return 404", done =>
    server
        .get("/")
        .expect(404)
        .end(done);
});


推荐阅读