Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js mocha process (_mocha) doesn't exit upon CTRL-C

When a mocha test takes a while to complete, hitting CTRL-C exits the mocha command but doesn't exit the test runner, "_mocha", and the test continues. Anybody know if this by design?

/* 
 * test/mocha-kill-test.js
 */
describe("Mocha Timeout Test", function() {

  this.timeout(10e3);

  it("should exit when hitting CTRL-C", function(done) {
    var count = 0;
    var timer = setInterval(function() {
      if (count++ < 10) {
        console.log("  WAIT " + count);
      } else {
        console.log("  DONE");
        clearInterval(timer);
        done();
      }
    }, 1e3);
  });

});

This test will run for 10 seconds and then exit. If you try to interrupt it from the terminal with a CTRL-C (or otherwise send it a SIGINT), the test runner will continue to run and you'll see something like this in your shell.

shell> mocha test/mocha-kill-test.js 

    WAIT 1
  WAIT 2
^Cshell>   WAIT 3
  WAIT 4
  WAIT 5
  WAIT 6
  WAIT 7
  WAIT 8
  WAIT 9
  WAIT 10
  DONE
․

  1 passing (11s)

I see that mocha is supposed to catch SIGINT and do a runner.abort() but this can't be the intended behavior, right?

node v0.10.26
mocha 1.18.2

like image 783
kelosh Avatar asked Apr 10 '14 02:04

kelosh


1 Answers

Quoting Mocha's supporter feedback about the issue: "you need to ensure the code Mocha is running actually stops at some point."

See link below for more details:

https://github.com/mochajs/mocha/issues/1362

According to other developers the code below will keep the Mocha process alive even after trying to kill it with CTRL+C:

echo "while(1);" > file.js
mocha -w file.js 
like image 155
b-s-d Avatar answered Sep 28 '22 06:09

b-s-d