'How to wait to execute await statement jest
I have a component that asynchronously loads configuration from external config server and awaits for the response, i have mocked the response object with global.fetch, however my console statement is not reached, test skip executing statements after await call. Below the line after await statement is not logged. How to make jest to wait the promise is resolved?
StyleWrapper.tsx
const StyleWrapper: FC = (props) => {
let async arrangeTiles = async() => {
const styleConf = await StyleWrapperHelper.getStyleConfig();
console.log(styleConf.defaultStyleName); // This line not executed during test!!!
};
return (
<Context.Provider value={{ arrangeTiles }}>
<div id="cnplStyleWrapper">{props.children}</div>
</Context.Provider>
);
};
StyleWrapper.test.tsx
test('Test Load style configuration', () => {
global.fetch = jest.fn(() =>
Promise.resolve({
json: () => Promise.resolve({"maxNumberOfTiles":20, "defaultStyleName": "CNPLCore"}),
})
) as jest.Mock;
const styleWrapper = mount(<StyleWrapper />);
styleWrapper.update();
expect(container.html()).toMatchSnapshot();
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
