Skip to content

Instantly share code, notes, and snippets.

@rogerwalt
Last active Feb 6, 2018
Embed
What would you like to do?
How to jest promises
/**
* example how to test promises using jest.
* https://facebook.github.io/jest/docs/en/asynchronous.html#promises
*/
function getPromise() {
return new Promise(function (success, reject) {
setTimeout(function() {
success('job is done');
}, 200);
});
}
function getPromiseThatWillReject() {
return new Promise(function (success, reject) {
setTimeout(function() {
reject('job is not done');
}, 200);
});
}
it('resolves a promise to assert value', () => {
return expect(getPromise()).resolves.toEqual('job is done');
});
it('rejects a promise to assert value', () => {
return expect(getPromiseThatWillReject()).rejects.toEqual('job is not done');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment