Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to terminate mocha test runs?

I want to terminate all the rest test cases in while a bunch test cases are being executed.

I am working with mocha on ui interface(on browser).

How do i terminate forcefully the test run?

Is there anything exactly 'opposite' of the call mocha.run(). something like 'mocha.stopRun()'. I could not find anything around this in documentation.

like image 972
codeofnode Avatar asked Nov 23 '22 09:11

codeofnode


1 Answers

I've not found a public API exported by mocha to ask it to terminate a suite at an arbitrary place. However, you can call mocha.bail() before you call mocha.run() to ask mocha to stop as soon as there is a test failure. If you want to be able to stop even if there is no failure, here's a way to do it:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8"/>
    <link href="node_modules/mocha/mocha.css" type="text/css" media="screen" rel="stylesheet" />
    <script type="text/javascript" src="node_modules/mocha/mocha.js"></script>
  </head>
  <body>
    <button id="terminate">Terminate Mocha</button>
    <div id="mocha"></div>
    <script>
      var terminate = document.querySelector("#terminate");
      var runner;
      var terminated = false;
      terminate.addEventListener("click", function () {
          if (runner) {
              // This tells the test suite to bail as soon as possible.
              runner.suite.bail(true);
              // Simulate an uncaught exception.
              runner.uncaught(Error("FORCED TERMINATION"));
              terminated = true;
          }
          return false;
      });

      mocha.setup("bdd");
      describe("test", function () {
          this.timeout(5 * 1000);
          it("first", function (done) {
              console.log("first: do nothing");
              done();
          });
          it("second", function (done) {
              console.log("second is executing");
              setTimeout(function () {
                  // Don't call done() if we forcibly terminated mocha.
                  // If we called done() no matter what, then if we terminated
                  // the run while this test is running, mocha would mark it
                  // as failed, and succeeded!
                  if (!terminated)
                      done();
              }, 2.5 * 1000);
          });
          it("third", function (done) {
              console.log("third: do nothing");
              done();
          });
      });
      runner = mocha.run();
    </script>
  </body>
</html>

If you click the "Terminate Mocha" button while mocha is busy with the 2nd test, it will cause the 2nd test to fail and the third test won't execute. You can verify this by looking at the output in the console.

If you were to use this as the means to stop your own test suite, you might want to register your asynchronous operations with the code run by the "Terminate Mocha" button so that these operations are terminated as soon as possible, if at all possible.

Note that runner.suite.bail(true) is not part of the public API. I've tried calling mocha.bail() at first but calling it in the middle of a test run did not work. (It would work only if called before calling mocha.run().) runner.uncaught(...) is also private.

like image 156
Louis Avatar answered Dec 06 '22 12:12

Louis