Last active
February 6, 2018 14:33
-
-
Save rogerwalt/31a29aaa7bdec7c5fa0fbf60a6a3c2e9 to your computer and use it in GitHub Desktop.
How to jest promises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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