小编yil*_*cum的帖子

对于异步测试和挂钩,我应该如何处理错误,确保调用“done()”;如果返回 Promise,请确保它得到解决

我正在使用摩卡进行测试

it('should allow a POST to /users', async function () {
        
        const res = await request.post('/users').send(firstUserBody);
    
        expect(res.status).to.equal(201);
        expect(res.body).not.to.be.empty;
        expect(res.body).to.be.an('object');
        expect(res.body.id).to.be.a('string');
        firstUserIdTest = res.body.id;
        
    });
Run Code Online (Sandbox Code Playgroud)

但我有一个错误

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Run Code Online (Sandbox Code Playgroud)

如果我使用 done() 执行此操作,该函数不是异步的,但它应该是

it('should allow a POST to /users', async function (done) {

        const res = await request.post('/users').send(firstUserBody);

        expect(res.status).to.equal(201);
        expect(res.body).not.to.be.empty;
        expect(res.body).to.be.an('object');
        expect(res.body.id).to.be.a('string');
        firstUserIdTest = res.body.id;
        done();
    });
Run Code Online (Sandbox Code Playgroud)

我应该怎么办 ?

mocha.js node.js typescript supertest

3
推荐指数
1
解决办法
5866
查看次数

标签 统计

mocha.js ×1

node.js ×1

supertest ×1

typescript ×1