callback inside useEffect not executing in JEST testing?Do I have to wait or something else. Repeat-callback inside useEffect not executing in JEST testing?Do I have to wait or something else. Repeat-callback inside useEffect not executing in JEST testing?Do I have to wait or something else.
1 Answer
Judging by the names of your function calls, yes, you would have to wait. fetch implies that you are performing an ansynchronous (a promise) call for data. That would, most likely, set some form of state. Doing that would rerender something (which takes a moment). Waiting for data changes is one of the hardest bits to wrap your head around when writing testing.
9 Comments
Sherlock
I am new in JEST testing.So can you please tell me how I can wrap the test for wait?
Sherlock
it("check for TrendsPageContainer", async () => { const props = { history: {}, location: {}, activeLang: "hi", activeLangEn: "Hindi", match: { params: {} } }; const component = await renderer.create( <Provider store={store}> <Router> <TrendsPageContainer {...props} /> </Router> </Provider> ); const tree = component.toJSON(); expect(tree).toMatchSnapshot(); });
Sherlock
this is not working
Sherlock
Yes I am using React-Testing-Library
Sherlock
it("check for TrendsPageContainer", async () => { const props = { history: {}, location: {}, activeLang: "hi", activeLangEn: "Hindi", match: { params: {} }, }; const component = renderer.create( <Provider store={store}> {" "} <Router> {" "} <TrendsPageContainer {...props} />{" "} </Router>{" "} </Provider> ); const tree = component.toJSON(); await waitFor(() => { expect(tree).toMatchSnapshot(); }); }); |