title: How to reset state beforeEach/afterEach jest test?
body: <p>My components all needed to be wrapped by a state provider in order for them to work, so when I render each component for testing I wrap them in the state provider. Like so:</p>
  
  <pre><code>&lt;StateProvider&gt;
    &lt;ThemeProvider theme={theme}&gt;
      &lt;Component_Being_Tested /&gt;
    &lt;/ThemeProvider&gt;
  &lt;/StateProvider&gt;
  </code></pre>
  
  <p>The issue I have is that when a component changes state during its test, it doesn't reset the state back to its initial state before the next test runs. This causes many tests to fail.</p>
  
  <p>For example, I have a password input component that includes a 'show/hide password' button. When clicked, it toggles a boolean state value <code>showPassword</code>. The initial state is set to <code>false</code>, which results in the input type being 'password'. On the first click, it changes the state to <code>true</code> and the component changes the input type to 'text'.</p>
  
  <p>When I run a test on this component that involves clicking the button, the first test runs just fine. However after this first test is run, the state for <code>showPassword</code> remains as <code>true</code> instead of resetting back to its initial <code>false</code> value. </p>
  
  <p>Therefore if I run another test that involves clicking the button, the test will fail as clicking the button will toggle the <code>showPassword</code> state back to its initial <code>false</code> whereas the test is expecting it to change to <code>true</code>.</p>
  
  <p>I've tried creating a function that resets state and running it in beforeEach(), like so:</p>
  
  <pre><code>beforeEach(() =&gt; useContext(Store).dispatch(resetState()));
  </code></pre>
  
  <p>However it fails as useContext is a React Hook and can only be called within a function component.</p>
  
  <p>How else can I reset state before/after each test?</p>