Sometimes, we want to test for a rejected promise using Jest.
In this article, we’ll look at how to test for a rejected promise using Jest.
How to test for a rejected promise using Jest?
To test for a rejected promise using Jest, we can use the rejects.toEqual
method.
For instance, we write
it('rejects...', () => {
const Container = createUserContainer(CreateUser);
const wrapper = shallow(<Container />);
expect(wrapper.instance().handleFormSubmit()).rejects.toEqual('error');
});
to call rejects.toEqual
with the promise rejection message we want to check for.
It assumes that handleFormSubmit
returns a promise.
Conclusion
To test for a rejected promise using Jest, we can use the rejects.toEqual
method.