'Mock MongoDB Realm serverless function using Jest and React Testing Library

I have a useEffect hook in a component in React that gets data from MongoDB Atlas using Realm serverless function(getData). Here is a snippet of the relevant code:

useEffect(() => {
    const getListings = async () => {
      const listings = await realmUser.functions.getData();
      setListings(listings);
    };

    getListings();
  });

When writing the unit test case for this component using React Testing Library and Jest, the challenge is how to mock this serverless function inside jest so that the response is returned locally instead of cloud DB. I am aware of how to mock Axios get calls, there are a ton of tutorials about it but could not find any way of mocking the serverless functions. I found this article that was very close to what I am looking for but still couldn't figure out things.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source