简体   繁体   English

当setInterval(…)运行时,Mocha测试套件永远不会结束

[英]Mocha test suite never ends when setInterval(…) running

I have a suite of tests which contains code with a setInterval(...) call. 我有一套测试,其中包含带有setInterval(...)调用的代码。

When running this code in mocha via a windows powershell command line, all tests will run, but the test runner will hang indefinitely afterwards. 通过Windows Powershell命令行在Mocha中运行此代码时,将运行所有测试,但此后测试运行程序将无限期挂起。

The command I am using is mocha "./unitTests/**/*.js" 我正在使用的命令是mocha "./unitTests/**/*.js"

Is there a way to force the test runner to close? 有没有办法迫使测试运行程序关闭?

Alternatively is there a way to determine that the code is running in a test environment so that I can disable my setInterval(...) calls? 另外,是否有一种方法可以确定代码是否在测试环境中运行,以便可以禁用setInterval(...)调用?

Example code: 示例代码:

// MODULE TO TEST
setInterval(function(){
    // do cleanup task
}, 1000000);

function testFunction(){
    return "val";
}

export {
    testFunction
}

// TEST MODULE
describe("Test setInterval", function() {  
    it("should finish", function() {
        testFunction().should.be.equal("val");

        // This test will complete and all others, but the entire suite will not
    });
});

The root cause is that Mocha by default considers the suite to be "done" when Node considers that the process is "done". 根本原因是,当Node认为进程“完成”时,Mocha 默认情况下将套件视为“完成”。 And by default Node waits for all uncleared timeouts and intervals to be "done" before calling the process "done". 并且默认情况下, Node在调用进程“完成”之前等待所有未清除的超时和间隔“完成”。 (An "uncleared" timeout or interval is one that has been created and never had clearTimeout/clearInterval called on it.) (“未清除的”超时或间隔是已创建的,从未对其调用clearTimeout/clearInterval 。)

A timeout is done when the callback you pass to setTimeout has finished executing, but an interval will never be done because by design it calls its callback forever and ever. 当您传递给setTimeout的回调完成执行时,将完成超时,但是永远不会完成间隔,因为根据设计,它会永远调用其回调。

Your options are: 您的选择是:

  1. Determine a condition by which the interval should be cleared, and clear it with clearInterval . 确定清除间隔的条件,然后使用clearInterval清除间隔。

  2. Use unref on the return value of setInterval . setInterval的返回值上使用unref This tells Node to ignore the interval when deciding whether the process is "done". 这告诉Node在确定进程是否“完成”时忽略间隔。

  3. Invoke mocha with the --exit option (introduced in Mocha 4), which forcibly exits the process when Mocha is done with the test suite. 使用--exit选项(在Mocha 4中引入)调用mocha ,当使用测试套件完成Mocha时,该选项将强制退出该过程。

I would use options 1 or 2. The third option works but if your test suite becomes more complex, it could hide problems you should be taking care of. 我将使用选项1或2。第三个选项有效,但是如果您的测试套件变得更加复杂,它可能会隐藏您应该注意的问题。 I would not use it if there is a more focused solution that can be used. 如果有可以使用的更集中的解决方案,我不会使用它。

you can use done() to end the test just like this example: 您可以像以下示例一样使用done()结束测试:

describe('User', function() {
  describe('#save()', function() {
    it('should save without error', function(done){
      var user = new User('Luna');
      user.save(function(err) {
        if (err) done(err);
        else done();
    });
   });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM