How to make Jest wait for all asynchronous code to finish execution before expecting an assertion

房东的猫 提交于 2019-11-27 17:12:05

问题


I am writing an integration test for for a React application, i.e. a test that tests many components together, and I want to mock any calls to external services.

The issue is that the test seems to execute before the async callback is executed causing my tests to fail.

Is there anyway around this? Can I somehow wait for call async code to finish?

Here is some bad pseudo code to illustrate my point.

I would like to test that when I mount Parent, its Child component render the content that came back from an external service, which i will mock.

class Parent extends component
{
     render ()
     {
         <div>
            <Child />
         </div>
     }
}
class Child extends component
{
     DoStuff()
     {
         aThingThatReturnsAPromise().then((result) => {
           Store.Result = result
         })
     }

    render()
    {
       DoStuff()
       return(<div>{Store.Result}</div>)


    }
}
function aThingThatReturnsAPromise()
{
     return new Promise(resolve =>{
          eternalService.doSomething(function callback(result) {
               resolve(result)
          }
    }

}

When I do this in my test, it fails because the It gets executed before the callback gets fired.

jest.mock('eternalService', () => {
    return jest.fn(() => {
        return { doSomething: jest.fn((cb) => cb('fakeReturnValue');
    });
});

describe('When rendering Parent', () => {
    var parent;

    beforeAll(() => {
        parent = mount(<Parent />)
    });

    it('should display Child with response of the service', () => {
        expect(parent.html()).toMatch('fakeReturnValue')
    });
});

How do I test this? I understand angular resolves this with zonejs, is there an equivalent approach in React?


回答1:


Here's a snippet that waits until pending Promises are resolved:

const flushPromises = () => new Promise(setImmediate);

Note that setImmediate is a non-standard feature (and is not expected to become standard). But if it's sufficient for your test environment, should be a good solution. Its description:

This method is used to break up long running operations and run a callback function immediately after the browser has completed other operations such as events and display updates.

Here's roughly how to use it using async/await:

it('is an example using flushPromises', async () => {
    const wrapper = mount(<App/>);
    await flushPromises();
    wrapper.update(); // In my experience, Enzyme didn't always facilitate component updates based on state changes resulting from Promises -- hence this forced re-render

    // make assertions 
});

I used this a lot in this project if you want some working real-world examples.




回答2:


I'm unaware of anything native to React to accomplish what you're looking for.

However, I was able to accomplish this in similar code by calling beforeAll()'s @done after setup was complete. See changes to your code below:

let setupComplete;
jest.mock('eternalService', () => {
    return jest.fn(() => {
        return { doSomething: jest.fn((cb) => { cb('fakeReturnValue'); setupComplete(); }) };
});
.
.
.
    beforeAll(done => {
        parent = mount(<Parent />)
        setupComplete = done;
    });
});

I've never used them, but of potential interest is Jest's runAllTicks and runAllImmediates.




回答3:


The Asynchronous nature of JavaScript is a major reason why unit testing JavaScript has taken so long to mature. Jest provides many patterns for testing asynchronous code described here: http://jestjs.io/docs/en/asynchronous.html I utilize async/await most extensively as it makes for extremely readable tests.

But since you are mocking the asynchronous method, there is no need for any asynchronous code. See below.

let parent
let eternalService = jest.fn(() => 'fakeReturnValue');

beforeEach(() => {
    parent = mount(<Parent />)
    parent.instance().externalService = externalService
})

describe('When rendering Parent', () => {
    it('should display Child with response of the service', () => {
        expect(parent.html()).toMatch('fakeReturnValue')
    });
});



回答4:


while the pseudo code can be refactored to follow React lifecycle (using componentWillMount() componentDidMount(), it would be much easier to test. However, below is my untested pseudo code to the changes for your test codes, feel free to test it out and update it. Hope it'll help you!

describe('When rendering Parent', () => {
    it('should display Child with the response of the service', function(done) => {
        const parent = mount(<Parent />);
        expect(parent.html()).toMatch('fakeReturnValue');
        done();
    });
});



回答5:


I would suggest you export aThingThatReturnsAPromise() from its module or file and then import it into your test case.

Since aThingThatReturnsAPromise() returns a promise, you can make use of the asynchronous testing features of Jest. Jest will wait for your promise to resolve and then you can make your assertions.

describe('When rendering Parent', () => {
    var parent;

    beforeAll(() => {
        parent = mount(<Parent />)
    });

    it('should display Child with response of the service', () => {
        expect.assertions(1);

        return aThingThatReturnsAPromise().then( () => {
            expect(parent.html()).toMatch('fakeReturnValue');
        });
    });
});

For more info, read how Jest handles test cases with Promises in the Jest Docs here



来源:https://stackoverflow.com/questions/44741102/how-to-make-jest-wait-for-all-asynchronous-code-to-finish-execution-before-expec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!