title: Mocking jest network - how do I not DRY my formatter?
body: <p>I have a network module to ping a legacy database with data in multiple formats, and I want to standardize it here in the network module before passing it into the application so my application can expect a certain format of data (don't want the old, poor formatting polluting my business logic). I'm struggling with how to pass mock data through as this network module, specifically as it relates to the formatter. Here's what I mean:</p>
  
  <pre class="lang-js prettyprint-override"><code>// User API Network Module
  // UserAPI.ts
  export const getUser = (uid: String, callback: (GetUserResponse) =&gt; void): void =&gt; {
    // Do network call here and format the data into a typescript type
    // matching the GetUserResponse structure by business logic expects
    callback(formattedData)
  }
  </code></pre>
  
  <p>In my test file, I can mock this call easily with:</p>
  
  <pre class="lang-js prettyprint-override"><code>import { getUser } from "./UserAPI"
  jest.mock("./UserAPI", () =&gt; ({
    getUser: (uid: String, callback: (GetUserResponse) =&gt; void) =&gt; {
      const mockedUserData = require("./mockUser.json")
      // But how do I format it here?
      return formattedMockedUserData
    },
  }))
  </code></pre>
  
  <p>I can create a formatter function in my <code>UserAPI.ts</code> file, export it, and run it in the jest mock, but I'm wondering if that's a best practice because it technically leaks the <code>UserAPI</code> implementation details. And I point that out only because no other file cares about how <code>UserAPI</code> formats things except <code>UserAPI</code>. If I have to leak it for testing purposes, I'll do that. But is there a better way to mock the network call and run it through a formatter without exposing additional implementation details?</p>
  
  <p>And please be gentle on my typescript - I come from both a JS and strongly typed background, but this is my first venture into using typescript :)</p>