Search code examples
javascriptnode.jsmocha.jsfunctional-testing

mocha global `before` that executes only 1 time


I'm using mocha for node.js functional testing.

There are several files in my test.

How can I run a piece of code for only one time before all tests start?

For example, I may have to set up a docker container before all tests start. Is it possible to do this with mocha?

The before hook runs 1 time for every test file. This doesn't meet my needs.


Solution

  • You can have 'root' level hooks, if you put them outside of any describe blocks. So you can put your relevant code in any files inside of the test folder.

    before(function() {
      console.log('before any tests started');
    });
    

    See the docs: http://mochajs.org/#root-level-hooks