jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
886 stars 118 forks source link

Can no longer flush promises since 1.6.0 #66

Closed doug-wade closed 6 years ago

doug-wade commented 6 years ago

I have some tests where I want to await the outcome of the results of fetch requests. The tests look like this

const manipulateDom = async () => {
    const wrapper = mount(<Provider store={store}><MyContainer /></Provider>);
    wrapper.find('button').simulate('click');
    await () => new Promise(resolve => setImmediate(resolve));
    return wrapper;
};
it('handles failures', async () => {
        fetch.mockReject(new Error('The request failed!'));

        const wrapper = await manipulateDom();

        expect(wrapper.find('results').html()).toContain('Failed to send contacts with error: {0}');
});

Note that I'm testing logic that does not return the promise, so I can't await it directly. Instead, I rely on the fact that the mock will resolve on next tick, and await setImmediate to wait for the next tick before making my assertions. This worked great, but it seems that the new Promise polyfill introduced in this PR has broken this behavior (afaict, since it also uses setImmediate as well, we can no longer be certain that has been resolved before our setImmediate. I'm a little hazy on the detail, though, as I haven't spelunked very far into the implementations of the promise polyfill).

nerfologist commented 6 years ago

After upgrading to 1.6.0, I'm getting a bunch of Possible Unhandled Promise Rejection: { Error: expect(received).toBe(expected) in my Jest test suite too. I believe it's connected to the problem described by @doug-wade.

jefflau commented 6 years ago

I'm going to merge this immediately and publish. Can you check if it works after?

nerfologist commented 6 years ago

@jefflau sure I'll test it first thing in the morning (CET). Thanks for your support šŸ‘

Edit: 1.6.1 fixes the issue for me šŸ‘ thanks @jefflau.

doug-wade commented 6 years ago

hi there @jefflau! We got this updated today, and our CI build is happily passing. Thanks so much for your quick response!