👇🏻 https://dev.to/jackcaldwell/mocking-es6-class-methods-with-jest-bd7

// We wanna mock this class
export class ProductsClient {
  async getById(id) {
    const url = `http://localhost:3000/api/products/{id}`;
    const response = await fetch(url);
    return await response.json();

// We wanna test this class
export class ProductManager {
  async getProductToManage(id) {
    const productsClient = new ProductsClient();
    const productToManage = await productsClient.getById(id)
      .catch(err => alert(err));
    return productToManage;
// In the file of testing class ProductManager
import { ProductsClient } from './ProductsClient';

// A "mock" getById() which returns "undefined" will be created
// But we want the mock function returns a value as we want

// assign a mock function to the ProductsClient's 'getById' method
const mockGetById = jest.fn();
ProductsClient.prototype.getById = mockGetById;
// We can make the mock function return what we want

// Restore the state of the original class
mockFn.mockClear() // or something like that (check the doc)