title: How to run function when any test fails - Jest
body: <p>I want to run a <code>function</code> / <code>task</code> whenever any <code>jest</code> test fails. Instead of wrapping all of my test's with <code>try</code> / <code>catch</code> or add an <code>if</code> check, is there a way I can utilize the <code>afterEach</code> ? </p>
  
  <p>If the test fails then I want it to fail, just run a separate function.</p>
  
  <p>For example: </p>
  
  <pre><code>test('nav loads correctly', async () =&gt; {
      const listItems = await page.$$('[data-testid="navBarLi"]')
  
      expect(listItems.length).toBe(4)
  
      if (listItems.length !== 4)
        await page.screenshot({path: 'screenshot.png'})
  
    })
  </code></pre>
  
  <p>This is adding an if check... But I want something more robust for all of my tests. </p>