超过 2000 毫秒的超时.对于异步测试和钩子,确保“done()"叫做;如果返回 Promise,请确保它已解决 [英] Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves

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

问题描述

我正在编写单元测试用例来测试 user.test.js 中 user.service.js 的方法,但是如果测试用例的数量增加,我会收到此错误:错误:超过 2000 毫秒超时.对于异步测试和钩子,确保调用了done()";如果返回一个 Promise,确保它得到解决."

I am writing unit test cases to test methods of user.service.js inside user.test.js but I am getting this error if the number of test cases increases: "Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves."

在这里,我将 plunker 链接附加到最低要求的文件:

Here I am attaching plunker link to minimum required files:

https://plnkr.co/edit/3us5ja?p=info

describe('testing', function() {
describe('random test cases 1', function() {
    it('Get all contacts 1', function() {
            return contactService.getAll((result) => {
                //console.log('all contact data: ' + 
JSON.stringify(result));
            });
    });
    it('Get all contacts 2', function() {
        return contactService.getAll((result) => {
            //console.log('all contact data: ' + JSON.stringify(result));
        });
    });
    it('Get all contacts 3', function() {
        return contactService.getAll((result) => {
            //console.log('all contact data: ' + JSON.stringify(result));
        });
    });
});

describe('random test cases 2', function() {
    it('Get all contacts 21', function() {
        return contactService.getAll((result) => {
            //console.log('all contact data: ' + JSON.stringify(result));
        });
    });
    it('Get all contacts 22', function() {
        return contactService.getAll((result) => {
            //console.log('all contact data: ' + JSON.stringify(result));
        });
    });
    it('Get all contacts23', function() {
        return contactService.getAll((result) => {
            //console.log('all contact data: ' + JSON.stringify(result));
        });
    });
});

describe('random test cases 3', function() {
    it('Get all contacts 31', function() {
        return contactService.getAll((result) => {
            //console.log('all contact data: ' + JSON.stringify(result));
        });
    });
    it('Get all contacts 32', function() {
        return contactService.getAll((result) => {
            //console.log('all contact data: ' + JSON.stringify(result));
        });
    });
    it('Get all contacts 33', function() {
        return contactService.getAll((result) => {
            //console.log('all contact data: ' + JSON.stringify(result));
        });
    });
});
});

有些文件在这里不相关.我尝试过的事情是:

Some of the files are not relevant here. The things what I had tried is:

1) 覆盖所有测试用例的超时,并在几个测试用例后面临同样的问题.

1) overrided timeout for all the test cases and facing the same issue after few test case.

2) 在每个测试用例中调用 done().

2) called done() in every test cases.

3) 在描述中配置超时,它,命令行也在 package.json 中.

3) configured timeout in described, it, command line also in package.json.

我正在返回来自 DAO 的承诺.所以当承诺得到解决时,我们无法预测.我们也无法预测测试用例的顺序,因此我们也无法增加测试套件或单个测试用例的超时时间.其他服务的测试用例也很少.那么,有没有办法解决这个问题.

I am returning promises from DAO. So when promise got resolve we can't predict. And order of test cases also we can't predict so we can't increase timeout also for test suite or individual test cases. There are few more test cases for other services also. So, is there any way to fix this issue.

推荐答案

这通常意味着测试在成功状态下没有调用 done().您可以通过调用 done() 来解决此问题.

This usually means that the test has not called done() in it's successful state. You can fix this by calling done().

describe('testing', () => {
  describe('random test cases 1', () => {
    it('Gets all contacts 1', (done) => {
        return contactService.getAll((result) => {
            done();
        });
    });
    it('Gets all contacts 2', (done) => {
      return contactService.getAll((result) => {
          done();
      });
    });
  });
  describe('random test cases 2', () => {
    it('Gets all contacts 21', (done) => {
      return contactService.getAll((result) => {
          done();
      });
    });
    it('Gets all contacts 22', (done) => {
      return contactService.getAll((result) => {
          done();
      });
    });
  });
});

如果您仍然看到错误,则可能是未调用回调,请仔细检查异步函数,例如contactService.getAll() 以上.另请仔细阅读https://mochajs.org/#asynchronous-code.

If you are still seeing the error it's probably that the callback is not being invoked, doublecheck the async function, e.g. contactService.getAll() above. Read also https://mochajs.org/#asynchronous-code carefully.

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

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