eslint-plugin-mocha icon indicating copy to clipboard operation
eslint-plugin-mocha copied to clipboard

How to refactor global hooks ?

Open BourgoisMickael opened this issue 5 years ago • 1 comments

I'd like to comply to mocha/no-top-level-hooks but I'm using global hooks to initialize my tests, an init.js file is executed before all of my tests:

const helper = require('./helper')

// GLOBAL HOOKS
before(async function () {
  // Make a connection to the database to be used in all tests
  await helper.db.start()
  // Run the server
  helper.app.run()
})

// Used to gracefully shutdown at the end
after(async function () {
  const server = helper.app.getServer()
  server.close()
  await helper.db.stop()
})

Note that in all my tests I have an afterEach hook to clean the db

With this implementation all my tests (107) are executed in 2 seconds. However, I tried to move that in beforeEach and afterEach in all my tests files to see if I can run the setup in each tests an now all the tests are executed in 2 minutes.

So should I just turn off this rule or is there a way to replace global hooks but keep nice performances ?

BourgoisMickael avatar Aug 14 '19 10:08 BourgoisMickael

If @BourgoisMickael is using "globals" to refer to Root Hook Plugins, then I have the same question and a potential suggestion. The suggestion being to add an argument to this rule definition that accepts an array of root hook file names, which will then be excluded from the linter parsing.

rosschapman avatar Jan 12 '21 20:01 rosschapman