Mocha test fails with error from "before all" hook

mocha.js, node.js

Solution

As I was posting this question, I stumbled on this bug which led me to the discovery that I need to make a call to `this.enableTimeouts(false)` in the beginning of the `before` function, like so:

let server
before(function(done) {
  this.enableTimeouts(false)  <----
  server = require('../app')
  server.initialize()
    .then(() => {
      console.info('listening on', process.env.PORT)
      server.listen(process.env.PORT, done)
    })
    .catch(err => {
      console.log(err)
      done(err)
    })
})

Hopefully this helps someone else a few hours of debugging.

Problem

From what I understand, the mocha test framework will throw this error if some error occurs in the `before` method: ``` > $(npm bin)/mocha test/*.js 1) "before all" hook ``` I've tried many different things to try and catch this error, but nothing seems to be working: ``` before(function(done) { server = require('../app') try { server.listen(process.env.PORT) done() // server.initialize() // .then(() => { // console.info('listening on', process.env.PORT) // server.listen(process.env.PORT, done) // }) // .catch(err => { // console.log(err) // done(err) // }) } catch (err) { console.log('outer error', err) done(err) } }) ``` From the commented out code, you can see that the server has an asynchronous operation I need to complete before the server actually starts listening. The only thing that seems to be working is to initialize the server outside of the `before` block and either call `done()` immediately in the `before` function, or pass a function that doesn't take a parameter. However this is still a problem since the server isn't initialized by the time the test starts. Maddeningly, this code works when I connect to a debugger, so I can't even inspect to see what's wrong.

Original source