Mocha单元测试:超时后如何强制停止执行测试

Mocha unit test: how to forcefully stop execution of test after timeout exceeded

我是运行使用mocha进行单元测试,我想强制超时,如果函数超过30000,所以我设置了this.timeout(30000);

为了验证它的工作,我在测试用例中放入了无限循环,即使超时超过测试也不会 return 返回。我希望此测试在超时后停止执行。

这是测试

    it('should forcefully timeout',function(done) {
    this.timeout(30000);
     while(1) {
        for(var i = 0; i < 10000; i++) {
            if( i % 2 == 0 ) {
                console.log("here");
                for(var i=0; i < 10000; i++) {
                    if( i % 2 == 0 ) {
                        console.log("here1");  
                    }
                }
            }
        }

      }
      done();         
  })


  1. async 运行 那些可能需要很长时间的函数。
  2. 添加退出标志,mocha 将在测试完成时终止进程,即使异步功能仍在 运行ning。例如:mocha \"test/**/*.js\" --exit