Mocha supports Promises out-of-the-box; You just have to return the Promise to it()‘s callback.
If the Promise resolves then the test passes.
In contrast, if the Promise rejects then the test fails. As simple as that.
Now, since async functions always implicitly return a Promise you can just do:
async function getFoo() {
return 'foo'
}
describe('#getFoo', () => {
it('resolves with foo', () => {
return getFoo().then(result => {
assert.equal(result, 'foo')
})
})
})
You don’t need done nor async for your it.
However, if you still insist on using async/await:
async function getFoo() {
return 'foo'
}
describe('#getFoo', () => {
it('returns foo', async () => {
const result = await getFoo()
assert.equal(result, 'foo')
})
})
In either case, DO NOT declare done as a function argument.
If you use any of the methods described above you need to remove done completely from your code. Passing done as an argument to it() callbacks hints to Mocha that you intent to eventually call it.
Using both Promises and done will result in:
Error: Resolution method is overspecified. Specify a callback or return a Promise; not both
The done method is only used for testing callback-based or event-based code. You shouldn’t use it if you’re testing Promise-based or async/await functions.