karma-runner / karma-mocha

A Karma plugin. Adapter for Mocha testing framework.
MIT License
379 stars 94 forks source link

Support for mocha root hooks #232

Open fgkolf opened 4 years ago

fgkolf commented 4 years ago

As of mocha v8, it supports root hooks https://mochajs.org/#root-hook-plugins in order to be able to globally define before and after hooks that run either once before/after your suite or before/after each test.

Following the guides to create such a root hook i ended up with the following configuration:

// hooks.js
exports.mochaHooks = {
  beforeAll() {
    // my setup code here
  }
};
// karma.config.js
 client: {
      mocha: {
        require: 'hooks.js'
      }
 }

But it didn't seem to work and my beforeAll hook never run. I also tried to specify a --require hooks.js in a mocha.opts file and then include it in my karma config as shown in the docs:

// karma.config.js
 client: {
      mocha: {
        opts: 'mocha.opts'
      }
 }

Again my global hook never run. Any insights on this?