jimijuu omastar
jimijuu omastar

Reputation: 117

how to set a particular test file as the first when running mocha?

Is there a way to set a particular test file as the first in mocha and then the rest of the test files can execute in any order.

Upvotes: 1

Views: 1299

Answers (3)

Dhruv Choudhary
Dhruv Choudhary

Reputation: 143

There is no direct way, but there is certainly a solution to this. Wrap your describe block in function and call function accordingly.

firstFile.js

function first(){
    describe("first test ", function () {
        it("should run first ", function () {

            //your code

       });
    });
   }

module.exports = {
     first
}

secondFile.js

 function second(){

      describe("second test ", function () {
         it("should run after first ", function () {

                    //your code

             })
          })
        }

  module.exports = {
     second
}

Then create one main file and import modules. main.spec.js

 const firstThis = require('./first.js)
 const secondSecond = require(./second.js)

 firstThis.first();
 secondSecond.second();

In this way you can use core javaScript features and play around with mocha as well. This is the solution I have been using since long. would highly appreciate if anyone would come with better approach.

Upvotes: 0

deerawan
deerawan

Reputation: 8443

One technique that can be used is to involve number in test filename such as

01-first-test.js
02-second-test.js
03-third-test.js

So by defining this, the test will be executed from first test until third test.

Upvotes: 2

Jim
Jim

Reputation: 4172

No. There is no guarantee your tests will run in any particular order. If you need to do some setup for tests inside of a given describe block, try using the before hook like so.

Upvotes: 1

Related Questions