代码之家  ›  专栏  ›  技术社区  ›  Nicholas Shanks

当Promise回调失败时Mocha超时

  •  0
  • Nicholas Shanks  · 技术社区  · 7 年前

    如果我有以下模块:

    module.exports = kontinue => {
      Promise.resolve({error:null})
        .then(o => {
          console.log('promise resolved');
          // say something goes wrong here
          if(true)
            kontinue({error:'promise resolved but something else went wrong'});
          else kontinue(o);
        })
        .catch(error => {
          console.log('caught error');
          kontinue({error:'promise rejected, or resolved but then continuation threw exception'})
        });
    };
    


    以及以下测试:

    const assert = require('assert').strict;
    const target = require('./the_above_code.js');
    
    it('should not timeout', (done) => {
      target((sut) => {
        console.log('continuation called');
        assert.ok(false); // the test for sut.error === what I expected was failing
        done();
      });
    });
    


    它输出:

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


    也许除了continuation(在使用此模块的代码中永远不会返回)之外,还有其他地方可以进行测试?

    Kernighan's maxim .

    0 回复  |  直到 7 年前
    推荐文章