Change default timeout for mocha

Asked
Active3 hr before
Viewed126 times

7 Answers

defaultchange
90%

Thanks for contributing an answer to Stack Overflow!, Stack Overflow help chat , Meta Stack Overflow , GitLab launches Collective on Stack Overflow

By default Mocha will read a file named test/mocha.opts that can contain command line arguments. So you could create such a file that contains:

--timeout 5000

Another way which may be better depending on your situation is to set it like this in a top level describe call in your test file:

describe("something", function() {
   this.timeout(5000);

   // tests...
});

Note that you cannot generally use an arrow function if you are going to call this.timeout (or access any other member of this that Mocha sets for you). For instance, this will usually not work:

describe("something", () => {
   this.timeout(5000); //will not work

   // tests...
});
load more v
88%

Whenever you run Mocha at the command line, it will read this file and set a timeout of 5 seconds by default.,This would allow you to set a timeout only on a per-file basis.,By default Mocha will read a file named test/mocha.opts that can contain command line arguments. So you could create such a file that contains:,The default timeout will be 2000 ms. It can be overwritten for partial test with a command line parameter:

mocha my - spec.js
load more v
72%

Returning a value from a node child process ,Returning a value from a node child process,The default timeout for mocha tests is 2000 ms. There are multiple ways to change this:,Tags: javascriptmochanodejstddunit testing

Change timeout for a single test case

describe("testing promises", function() {
   this.timeout(5000);
   it('test1', function() {
      ...
   });
});
load more v
65%

With its default “BDD”-style interface, Mocha provides the hooks before(), after(), beforeEach(), and afterEach(). These should be used to set up preconditions and clean up after your tests.,The BDD interface provides describe(), context(), it(), specify(), before(), after(), beforeEach(), and afterEach().,The QUnit-inspired interface matches the “flat” look of QUnit, where the test suite title is defined before the test-cases. Like TDD, it uses suite() and test(), but resembling BDD, it also contains before(), after(), beforeEach(), and afterEach().,All hooks (before(), after(), beforeEach(), afterEach()) may be sync or async as well, behaving much like a regular test-case. For example, you may wish to populate database with dummy content before each test:

Install with npm globally:

$ npm install--global mocha
load more v
75%

describe("testing promises", function() {
   this.timeout(5000);
});
load more v
40%

Apparently, when setting up a MPP project to compile over NodeJS, the default option for testing is Mocha. Apparently, Mocha has a 2000ms default timeout which is breaking my tests.,Another interesting thing that when I compile my test code to the regular js file and run in browser it took less than 200ms for running. But when I run tests I have an error with 2000ms timeout.,In your main project’s directory (where all the src, build.gradle.kts etc are) create new dir, call it karma.config.d inside a js file called karma.config.js with following content:,From what I can understand, Mocha can be configured by editing the package.json file generated by Gradle, but I could not find any way to configure the content of the package.json file in my build.gradle.kts. Am I missing something or it is a missing feature?

I think I figured this out:

kotlin {
   sourceSets {

      // ...

      js {

         nodejs { // or `browser`
            testTask {
               useMocha {
                  timeout = // mochaTimeout here as string
               }
            }
         }

         // ...

      }
   }
load more v
22%

The -t, --timeout <ms> option allows you to set the timeout for your test cases. Mocha.js uses a default of 2 seconds. You can set the timeout by specifying the number of milliseconds or a value with an s suffix to specify the time in seconds.,To disable timeouts entirely, you can use --no-timeouts, which is equivalent to --timeout 0.,It’s time for you to write your first test suite and run the tests with Mocha. To start, you must implement a sum() function that:,Note that, calling this.timeout() with 0 disables timeout completely.

/* package.json */

{
   "scripts": {
      "test": "mocha"
   }
}
load more v

Other "default-change" queries related to "Change default timeout for mocha"