简体   繁体   中英

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

I am running unit test using mocha, I want to forcefully timeout, if the function exceeds 30000, so I have set this.timeout(30000);

to verify its working I put infinite loop inside test case, even timeout exceeds test doesn't return back. I want this test to stop executing after timeout.

Here is the test

    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 run those functions which may take long time.
  2. add exit flag, mocha will terminate process when test finished even async function still running. ex: mocha \"test/**/*.js\" --exit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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