title: Mocking a Typescript Class That Makes External API Calls
body: <p>So I've done a lot of reading/searching, and can't seem to figure this one out.</p>
  <p>I have a typescript class that looks like this</p>
  <pre><code>import * as Shopify from 'shopify-api-node';
  export default class ShopifyService {
    private readonly shopify: Shopify;
  
    constructor(
      config: Shopify.IPublicShopifyConfig | Shopify.IPrivateShopifyConfig,
    ) {
      this.shopify = new Shopify({ ...config, apiVersion: SHOPIFY_API_VERSION });
    }
  
    public async getCurrentBulkOperation(): Promise&lt;
      ICurrentBulkOperation | undefined
    &gt; {
      const response = await this.shopify.graphql(bulkOperationStatusQuery);
      return response?.currentBulkOperation;
    }
  }
  </code></pre>
  <p>This class just consists of multiple functions that make graphQL calls (this.shopify.graphql) and returns the results.</p>
  <p>I have another file called polly.ts</p>
  <pre><code>export const pollyWolly = {
  
     async cons(....) {
           const shopify = new ShopifyService({ accessToken, shopName });
           const m = await shopify.getCurrentBulkOperation();
           // do stuff with m. Ex: get f from m, send f to some queue, etc etc.
     }
  
  }
  </code></pre>
  <p>I am writing tests for pollyWolly. For example, the test could be hey when cons runs and shopify.getCurrentBulkOperation returns null, then make sure a message is send to some queue etc etc.</p>
  <p>I want to MOCK shopify.getBulkOperation --&gt; I don't want to make actual API calls to shopify... And I want to mock with different values. For example, in one test case i want to imagine that getBulkOp returned null. In another I want to image that getBulkOperation returned an object with specific fields etc.</p>
  <p>I've read a lot of stackoverflow posts, a lot of articles, but can't seem to quite get it.</p>
  <p>Help appreciated.</p>