Mocha test fails with error from “before all” hook

后端 未结 2 1765
走了就别回头了
走了就别回头了 2021-01-06 00:53

From what I understand, the mocha test framework will throw this error if some error occurs in the before method:

> $(npm bin)/mocha test/*.j         


        
相关标签:
2条回答
  • 2021-01-06 01:38

    It has been a little time since it was asked but for the future readers here we go:

    You can run your test with mocha test/*.js --timeout 5000 command. That's it.

    But if you run your test with npm test command, just edit your package.json file like that:

    "scripts": {
        "start": "nodemon ./bin/www",
        "test": "mocha --exit --recursive --timeout 5000" // add this line
      }
    
    0 讨论(0)
  • 2021-01-06 01:56

    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.

    0 讨论(0)
提交回复
热议问题