对于异步测试和挂钩,请确保"done()"叫做;如果返回承诺,请确保它解决了 [英] For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves

查看:61
本文介绍了对于异步测试和挂钩,请确保"done()"叫做;如果返回承诺,请确保它解决了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进行测试时,我得到了对nodejs的测试,但我收到未声明的完成函数错误.

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

我的测试代码是,我已经完成了回叫,但是仍然出现错误,无法调用done();

    it('remove existing subdocument', (done) => {
    const Vic = new User({
      name: 'Vic',
      posts: [{ title: 'Leaning Nodejs' }]
    });

    vic.save()
      .then(() => User.findOne({ name: 'Vic' }))
      .then((user) => {
        const post = user.posts[0];
        post.remove();
        return user.save();
      })
      .then(() => User.findOne({ name: 'Vic' }))
      .then((user) => {
        assert(user.posts.length === 0);
        done();
      });
  });

解决方案

我遇到了同样的问题,@ MFAL的评论链接很有帮助.我正在扩展它.

当存在错误/不正确的断言时,promise内将引发错误.这导致承诺被拒绝.一旦拒绝完成,就永远不会调用,并且摩卡报告超时. 我通过编写.catch块并将其与promise链接来解决了这个问题:

          it('resolves', (done) => {
            fooAsyncPromise(arg1, arg2).then((res, body) => {
                expect(res.statusCode).equal(incorrectValue);
                done();
            }).catch(done);
         });

维茨的博客中提到的其他方式是:

链接then(done, done),该c2同时处理承诺的解决和拒绝.

         it('resolves', (done) => {
           resolvingPromise.then( (result) => {
             expect(result).to.equal('promise resolved');
           }).then(done, done);
         });

兑现承诺:

        it('resolves', () => {
          return resolvingPromise.then( (result) => {
            expect(result).to.equal('promise resolved');
          });
        });

使用异步/等待:

        it('assertion success', async () => {
          const result = await resolvingPromise;
          expect(result).to.equal('promise resolved'); 
        });

I have this test of nodejs when testing I get a error of done function not declared.

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

My test code is, I have the done call back but still getting the error to call the done();

    it('remove existing subdocument', (done) => {
    const Vic = new User({
      name: 'Vic',
      posts: [{ title: 'Leaning Nodejs' }]
    });

    vic.save()
      .then(() => User.findOne({ name: 'Vic' }))
      .then((user) => {
        const post = user.posts[0];
        post.remove();
        return user.save();
      })
      .then(() => User.findOne({ name: 'Vic' }))
      .then((user) => {
        assert(user.posts.length === 0);
        done();
      });
  });

解决方案

I was facing the same issue, @MFAL's link in comment helped. I am expanding upon it.

When there is an error/incorrect assertion an error is raised inside the promise. This leads to promise rejection. Once rejected done is never called and mocha reports time out. I solved this by writing a .catch block and chaining it with the promise:

          it('resolves', (done) => {
            fooAsyncPromise(arg1, arg2).then((res, body) => {
                expect(res.statusCode).equal(incorrectValue);
                done();
            }).catch(done);
         });

Other ways as mentioned in the Wietse's blog are:

To chain a then(done, done) which handles both resolve and reject of the promise.

         it('resolves', (done) => {
           resolvingPromise.then( (result) => {
             expect(result).to.equal('promise resolved');
           }).then(done, done);
         });

Return a promise:

        it('resolves', () => {
          return resolvingPromise.then( (result) => {
            expect(result).to.equal('promise resolved');
          });
        });

Use async/wait:

        it('assertion success', async () => {
          const result = await resolvingPromise;
          expect(result).to.equal('promise resolved'); 
        });

这篇关于对于异步测试和挂钩,请确保"done()"叫做;如果返回承诺,请确保它解决了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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