Loving Async/Await even more…

Discovering testing promises with mocha and async/await

I’m a big fan of async/await and can’t wait to use it in production code after it goes LTS this October.

I’ve just discovered another big plus of async/await: testing promises

No more need for things like ‘chai-as-promised’, which while they simplify the process greatly; do not, in my opinion at least, make for clear and easily readable code.

Nor do you need to call done(), as an AsyncFunction returns an instance of a Promise naturally.

All you have to do is pass an AsyncFunction as the callback in your mocha it functions.

i.e. see below

'use strict';
const {expect} = require('chai');
const prom = () => new Promise((resolve, reject) => {
setTimeout(() => resolve('success'), 1000)
});
describe('promise example', () => {
it('should handle a resolved promise', async () => {

let result = await prom()

expect(result).to.equal('success');

})
});