Skip to content

Instantly share code, notes, and snippets.

@rogerwalt
Last active February 6, 2018 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerwalt/31a29aaa7bdec7c5fa0fbf60a6a3c2e9 to your computer and use it in GitHub Desktop.
Save rogerwalt/31a29aaa7bdec7c5fa0fbf60a6a3c2e9 to your computer and use it in GitHub Desktop.
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