title: How do you use Jest to test img.onerror
body: <p>I've created a React component that loads an image and determines if the image loaded successfully or not.</p>
  
  <pre><code>import React from 'react';
  import PropTypes from 'prop-types';
  import { LOADING, SUCCESS, ERROR } from '../helpers';
  
  class Image extends React.Component {
    static propTypes = {
      onError: PropTypes.func,
      onLoad: PropTypes.func,
      src: PropTypes.string.isRequired,
    }
  
    static defaultProps = {
      onError: null,
      onLoad: null,
    }
  
    constructor(props) {
      super(props);
      this.state = { imageStatus: LOADING };
      this.initImage();
    }
  
    componentDidMount() {
      this.image.onload = this.handleImageLoad;
      this.image.onerror = this.handleImageError;
      this.image.src = this.props.src;
    }
  
    initImage() {
      this.image = document.createElement('img');
      this.handleImageLoad = this.handleImageLoad.bind(this);
      this.handleImageError = this.handleImageError.bind(this);
    }
  
    handleImageLoad(ev) {
      this.setState({ imageStatus: SUCCESS });
      if (this.props.onLoad) this.props.onLoad(ev);
    }
  
    handleImageError(ev) {
      this.setState({ imageStatus: ERROR });
      if (this.props.onError) this.props.onError(ev);
    }
  
    render() {
      switch (this.state.imageStatus) {
        case LOADING:
          return this.renderLoading();
        case SUCCESS:
          return this.renderSuccess();
        case ERROR:
          return this.renderError();
        default:
          throw new Error('unknown value for this.state.imageStatus');
      }
    }
  }
  
  export default Image;
  </code></pre>
  
  <p>I'm trying to create a test using Jest + Enzyme to test when an image fails to load.  </p>
  
  <pre><code>  it('should call any passed in onError after an image load error', () =&gt; {
      const onError = jest.fn();
      mount(&lt;Image {...props} src="crap.junk"} onError={onError} /&gt;);
      expect(onError).toHaveBeenCalled();
    });
  </code></pre>
  
  <p>No matter what I do, Jest always finds a way to successfully render the image.  Even setting src to false still renders an image somehow.  Does anyone know how in the heck you can force jest to fail an image load?</p>