节点中的 .done() 和 .end() 函数有什么区别以及何时使用它们?

what is the difference between .done() and .end() function in node and when they shall be used?

我在 mocha 和 Chai 中编写 API 测试脚本时试图了解 end() 函数的用例。同时,我很困惑我是否应该在这里使用 done() 函数以及 .end().done() 之间的确切区别是什么。

代码如下:

describe("Suite", () => {
    it('Post Test case', (done) => {
        request('https://reqres.in')
        .post('/api/users')
        .send({
            "name": "morpheus",
            "job": "leader"
        })
        .set('Accept', 'application/json')
        .expect(200,'Content-Type', /json/)
        .then((err,res) => {
            console.log(JSON.stringify(err))
            console.log(JSON.stringify(res.body))
            console.log(JSON.stringify("           "))

        })
        done();
    });
    it('Put Test case', (done) => {
        request('https://reqres.in')
        .put('/api/users/2')
        .send({
            "name": "morpheus",
            "job": "zion residents"
        })
        .expect(200)
        .end((err, res) => {
            console.log(JSON.stringify(err))
            console.log(JSON.stringify(res.body))
            console.log(JSON.stringify("           "))

        })
        done();
    })
})

你有点混淆了东西。

endexpressjs framework 的一个方法,它 结束 服务器响应。

donemocha test function的一个参数。当您完成 异步 测试时调用此参数,让 mocha 知道您的异步代码已执行完毕,它可以继续进行另一个测试。

在你的情况下,你需要两者。