title: How to mock callback functions with jest
body: <p>I'm trying to mock a custom function with jest but I'm having problems with it.</p>
  
  <p>This is my function:</p>
  
  <pre><code>export const resizeImage = (file, fileName, callback) =&gt; {
    const MAX_WIDTH = avatarImage.maxWidth;
    const MAX_HEIGHT = avatarImage.maxHeight;
    const img = document.createElement('img');
    img.src = window.URL.createObjectURL(file);
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');
  
    img.onload = () =&gt; {
      const sizes = internalResizeImage(img, MAX_WIDTH, MAX_HEIGHT);
  
      canvas.width = sizes.width;
      canvas.height = sizes.height;
      ctx.drawImage(img, 0, 0, sizes.width, sizes.height);
      return callback(dataURItoFile(canvas.toDataURL(), fileName));
    };
  };
  </code></pre>
  
  <p>I called like this:</p>
  
  <pre><code>resizeImage(acceptedFiles[0], this.props.user.id, (res) =&gt; {
    //dostuff
  });
  </code></pre>
  
  <p>In my test I'm mocking it like this:</p>
  
  <pre><code>let mockResizeImage = jest.fn();
  
  jest.mock('../../utilities/imageUtils', () =&gt; ({
    resizeImage: () =&gt; mockResizeImage
  }));
  </code></pre>
  
  <p>I want mockResizeImage to be a callback and then in my test change the returning values:</p>
  
  <pre><code>it('should call handleDrop and accept files', () =&gt; {
      //mockResizeImage.mockReturnValue('something');
  
      const instance = shallow(mockComponent()).instance();
      const acceptFilesMock = ['test'];
  
      instance.handleDrop(acceptFilesMock);
  
      expect(clickSpy).toHaveBeenCalledTimes(1);
    });
  </code></pre>
  
  <p>If it were a promise, all good, but it's a callback and I don't know what I'm doing wrong.</p>
  
  <p>Thanks.</p>