title: Jest - test try-catch block
body: <p>I'm trying to test a try catch block using Jest. E.g.</p>
  
  <pre><code>// filename: foo.js
  
  const foo = () =&gt; {
    let js = 'somejs'
    try {
      eval(js)  
    } catch (e) {
      document.location = 'redirect/to/page'
      return false
    }
    return true
  }
  
  export default foo
  </code></pre>
  
  <p>Do you mock <code>eval</code> (not my choice to use eval. It's my bosses code) and then get it to throw? If so, how would you do that? E.g. </p>
  
  <pre><code>eval = jest.fn()    // doesn't work 
  </code></pre>
  
  <p>So the test code might be:</p>
  
  <pre><code>// filename: foo.spec.js
  
  import foo from './foo'
  eval = jest.fn()
  
  describe('foo', () =&gt; {
    describe('when eval succeeds', () =&gt; {
      it('performs an eval', {
      const result = foo()
      expect(result).toEqual(true)
    })
    describe('when eval fails', () =&gt; {
      beforeEach(() =&gt; {
        eval.mockImplementation(() =&gt; {throw 'error'})
      })
      it('catches the error and redirects', () =&gt; {
        const result = foo()
        expect(result).toEqual(false)
      })
    })
  })
  </code></pre>
  
  <p>What would the mocking and test code look like?</p>