Mocha before function - lambda without curly braces errors; lambda with curly braces works

时间:2017-04-09 23:47:23

标签: javascript node.js mocha mockgoose

This code makes my mocha tests pass without error:

before(done => {
  mockgoose
    .prepareStorage()
    .then(() => mongoose.connect('mongodb://example.com/TestingDB'))
    .then(done)
})

it('passes', done => done())

But removing the curly braces in the before block causes the error:

before(done =>
  mockgoose
    .prepareStorage()
    .then(() => mongoose.connect('mongodb://example.com/TestingDB'))
    .then(done)
)

it('passes', done => done())

1) "before all" hook

0 passing (2s)
1 failing

1)  "before all" hook:
    Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.
    at process._tickCallback (internal/process/next_tick.js:109:7)

Does anyone know why? If more context is needed, I can oblige.

1 个答案:

答案 0 :(得分:3)

It says so right there, you weren't returning anything before, you were just using done to specify when the task is done. Now you are returning a Promise (the result of the mockgoose call i'd assume) and it's confusing mocha.