让摩卡等待运行下一个测试

时间:2022-04-24 19:20:59

Got some mocha tests that require data from prior function calls, but then because it's using a webservice, and would like it to literally wait for a predetermined amount of time before running the next test, something like:

有一些mocha测试需要来自之前函数调用的数据,但是因为它使用的是webservice,并且希望它在运行下一个测试之前等待预定的时间,比如:

var global;

it('should give some info', function(done) {
  run.someMethod(param, function(err, result) {
    global = result.global
  done();
  });
});

wait(30000); // basically block it from running the next assertion

it('should give more info', function(done) {
  run.anotherMethod(global, function(err, result) {
    expect(result).to.be.an('object');
  done();
  });
});

Any ideas would be appreciated. Thanks!

任何想法都会受到欢迎。谢谢!

2 个解决方案

#1


14  

setTimeout definitely could help, but may be there is a "cleaner" way to do that. docs actually says to use this.timeout(delay) to avoid timeout errors while testing async code, so be careful.

setTimeout肯定会有所帮助,但是可能会有一个“更干净”的方法来实现它。实际上,文档说要使用this.timeout(delay)来避免在测试异步代码时发生超时错误,所以要小心。

var global;

it('should give some info', function(done) {
  run.someMethod(param, function(err, result) {
    global = result.global
  done();
  });
});



it('should give more info', function(done) {
    this.timeout(30000);

    setTimeout(function () {
      run.anotherMethod(global, function(err, result) {
        expect(result).to.be.an('object');
        done();
      });
    }, 30000);
 });

#2


8  

While this.timeout() will extend the timeout of a single test, it's not the answer to your question. this.timeout() sets the timeout of your current test.

虽然this.timeout()将延长单个测试的超时时间,但这不是问题的答案。this.timeout()设置当前测试的超时。

But don't worry, you should be fine anyway. Tests are not running in parallel, they're done in series, so you should not have a problem with your global approach.

但别担心,你应该没事的。测试不是并行运行的,它们是串联运行的,所以您不应该对全局方法有问题。

#1


14  

setTimeout definitely could help, but may be there is a "cleaner" way to do that. docs actually says to use this.timeout(delay) to avoid timeout errors while testing async code, so be careful.

setTimeout肯定会有所帮助,但是可能会有一个“更干净”的方法来实现它。实际上,文档说要使用this.timeout(delay)来避免在测试异步代码时发生超时错误,所以要小心。

var global;

it('should give some info', function(done) {
  run.someMethod(param, function(err, result) {
    global = result.global
  done();
  });
});



it('should give more info', function(done) {
    this.timeout(30000);

    setTimeout(function () {
      run.anotherMethod(global, function(err, result) {
        expect(result).to.be.an('object');
        done();
      });
    }, 30000);
 });

#2


8  

While this.timeout() will extend the timeout of a single test, it's not the answer to your question. this.timeout() sets the timeout of your current test.

虽然this.timeout()将延长单个测试的超时时间,但这不是问题的答案。this.timeout()设置当前测试的超时。

But don't worry, you should be fine anyway. Tests are not running in parallel, they're done in series, so you should not have a problem with your global approach.

但别担心,你应该没事的。测试不是并行运行的,它们是串联运行的,所以您不应该对全局方法有问题。