Asynchronous Unit Tests

const doSomethingAsync = () => {
const p = new Promise(function(resolve, reject) {
setTimeout(() => {
resolve('OK');
}, 300);
});
return p;
};
describe('when using the done function', () => {
it('should pass', done => {
doSomethingAsync()
.then(msg => {
expect(msg).toEqual('OK');
})
.then(done, done);
});
});
describe('when returning a Promise', () => {
it('should pass', () => {
return doSomethingAsync().then(msg => {
expect(msg).toEqual('OK');
});
});
});

--

--

Previously Paris and Geneva, currently New York. Can also be found at https://dev.to/arnaud

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store