title: Testing functions that have database calls with Jest
body: <p>I'm new to Jest and code testing in general.  I would like to test a function that retrieves data from a mongodb database using mongoose.  When I run the test all the calls to the db inside the function I'm testing return null.  What's the recommended way to test functions that need to retrieve data from a database?</p>
  <p>Let me know if you need code samples or more information.</p>
  <h3>Update:</h3>
  <p>Here's my code:</p>
  <p>code.js</p>
  <pre><code>...
  function buildText(listing, messageRule, reservation, isReview = false, isLastMinuteMessage = false) {
      return new Promise(async function(resolve) {
          const name = reservation.name;
          try {
              message = await Message.findOne({
                  listingID: listing._id,
                  messageRuleID: messageRule._id,
                  reservationID: reservation._id,
                  message: {$exists: true}
              });
          } catch (error) {
              console.error(error);
          }
          if (message) {
              text = message.message;
          } else {
              text = messageRule.message;
              if (isLastMinuteMessage) {
                  text = messageRule.lastMinuteMessage;
              }
          }
          text = text.replace(/{{Guest Name}}/g, name);
          resolve(text);
      });
  }
  ...
  </code></pre>
  <p>code.test.js</p>
  <pre><code>const Code = require(&quot;./code&quot;);
  describe(&quot;funcs&quot;, () =&gt; {
      it(&quot;buildText&quot;, async () =&gt; {
          let listing = {
              _id: &quot;1324&quot;,
              pricingEnabled: true,
              listingFound: true,
              hideListing: null
          };
          let messageRule = {
              _id: &quot;3452345&quot;,
              minNights: 1,
              reviewEnabled: false,
              disableMessageAfterReview: false,
              message: 'test',
              lastMinuteMessage: 'test2'
          };
          let reservation = {
              _id: &quot;63452345&quot;,
              hasLeftReview: true,
              hasIssues: false
          };
          let isReview = false;
          let isLastMinuteMessage = false;
          let response = await Code.buildText(listing, messageRule, reservation, isReview, isLastMinuteMessage);
          expect(response).toMatchSnapshot();
      });
  });
  </code></pre>
  <p>The trouble I'm having is with the <code>message = await Message.findOne({</code> line in my code file.  It always returns null but I need to be able to tell it to return true or false.  How can I do that?</p>