promise & mocha: done() in before or not?

前端 未结 3 1379
生来不讨喜
生来不讨喜 2021-01-19 23:42

I am reading some tutorials on promise tests in mocha. There is a piece of codes:

before(function(done) {
  return Promise.resolve(save(article)).then(functi         


        
3条回答
  •  孤城傲影
    2021-01-20 00:14

    I am not sure if I understood 100% the question, but the tests will not start until done is called.

     beforeEach(function(done) {
        setTimeout(function() {
          value = 0;
          done();
        }, 1);
      });
    

    This test will not start until the done function is called in the call to beforeEach above. And this spec will not complete until its done is called.

      it("should support async execution of test preparation and expectations", function(done) {
        value++;
        expect(value).toBeGreaterThan(0);
        done();
      });
    

    You don't have to pass done in your example, just:

    before(function() {
      return Promise.resolve(save(article));
    });
    

    If you do pass done the test runner will expect to be called before continue, otherwise it will probably throw a timeout error.

提交回复
热议问题