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
Arnaud Dostes

Arnaud Dostes

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

More from Medium

Testing DOM mutations with Jest

Learn TypeScript With Examples

Split thy code, increaseth performance.

Migrating from Moment.js to Luxon