Unit Testing with Promises
...
Some tips for testing promises:
...
Testing that a Promise is resolved
Code Block |
---|
// Remember to use the 'async', 'await', and 'expect' below.
it('tests that someMethodThatReturnsAPromise rejects with an error', async () => {
// first, set up data such that the promise should be resolved
// second, call the method
await expect(someMethodThatReturnsAPromise()).resolves.toBe(undefined);
}); |
Testing that a Promise is rejected
Code Block |
---|
// Remember to use the 'async', 'await', and 'expect' below.
it('tests that someMethodThatReturnsAPromise rejects with an error', async () => {
// first, set up data such that the promise should be rejected
// second, call the method
await expect(someMethodThatReturnsAPromise()).rejects
.toThrow(new Error('The error sent to the reject.'));
}); |
Mock that a Promise was resolved
Code Block |
---|
jest.spyOn(object, 'methodWithPromise').mockResolvedValue(undefined); |
Mock that a Promise was rejected
Code Block |
---|
jest.spyOn(object, 'methodWithPromise').mockRejectedValue(new Error('test')); |
Mocking Top-level Objects (e.g. document)
Code Block |
---|
jest.spyOn(global.document, 'referrer', 'get').mockReturnValue(loginUrl); |
Unit Test Debugging
Note |
---|
Not sure how to do this from a container, but there should be a way. These instructions assume you are running node from your host. |
...