How to run a mocha test only after the previous asynchronous test passed?

Using the javascript mocha testing environment, I want to be able to run several tests (all asynchronous) only after the test passed before passes.

I do not want them to embed these tests into each other.

describe("BBController", function() { it("should save", function(done) {}); it("should delete", function(done) {}); }) 
+7
javascript testing mocha
source share
2 answers

Use the --bail . Be sure to use at least mocha 0.14.0. (I tried this with older versions without success.)

Firstly, you have nothing to do for mocha to start a test only after the previous one is finished. This is how mocha works by default. Save this value in test.js :

 describe("test", function () { this.timeout(5 * 1000); // Tests time out in 5 seconds. it("first", function (done) { console.log("first: do nothing"); done(); }); it("second", function (done) { console.log("second is executing"); // This test will take 2.5 seconds. setTimeout(function () { done(); }, 2.5 * 1000); }); it("third", function (done) { console.log("third is executing"); // This test will time out. }); it("fourth", function (done) { console.log("fourth: do nothing"); done(); }); }); 

Then execute it with

 mocha -R spec test.js 

You will not see the fourth test run until:

  • The first and second tests are completed.
  • The third test has a timeout.

Now run it with

 mocha -R spec --bail test.js 

Mocha stops as soon as test 3 fails.

+2
source share

if your tests are configured correctly, only by testing a small part of the business logic, you can run tests asynchronously, but they should not delay other tests. The way to get the test to complete is to do the following:

 describe("BBController", function() { it("should save", function(done) { // handle logic // handle assertion or other test done(); //let mocha know test is complete - results are added to test list }); it("should delete", function(done) { // handle logic // handle assertion or other test done(); //let mocha know test is complete - results are added to test list }); }); 

again, no test needs to wait for another test to run, if you have this problem, you should look at how to impose dependency injection or prepare your tests using the before method.

-4
source share

All Articles