title: BeforeAll vs. BeforeEach. When to use them?
body: <p>I was recently looking over a co-workers code and I realized that he implements a jest function in a BeforeAll function at the top of the describe call, and then creates a data object in a beforeEach function.  This made me wonder, what exactly are the differences between BeforeAll and BeforeEach.</p>
  
  <p>It was time... I went to Google!!  I did find some articles that helped shed some light on some of the functionality differences between the two.</p>
  
  <p><strong>Findings 1</strong>: <a href="http://breazeal.com/blog/jasmineBefore.html" rel="noreferrer">http://breazeal.com/blog/jasmineBefore.html</a></p>
  
  <p><strong>Findings 2</strong>: <a href="https://stackoverflow.com/questions/20295578/difference-between-before-beforeclass-beforeeach-and-beforeall">Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll</a></p>
  
  <p>Given the articles I found that BeforeAll is called once and only once.  While the BeforeEach is called before each individual test.  Which was great!  I now had a better idea of when it was being called!  </p>
  
  <p>I also found out that the BeforeAll is best used for initializing code. Which makes perfect sense!  Initialize it once.  Boom, you're done.</p>
  
  <p>My confusion I am having is when is something initialized and when is it not?  I have found that BeforeEach in our code is used more often than not.  What I am curious about is what kind of code is considered to be "initializing" code, vs whatever code should be in the BeforeEach.</p>
  
  <p>An example from our code below:</p>
  
  <pre><code>    beforeAll((done) =&gt; {
        // Mocking method from within Box file
        transferBoxPlanSpy = jest.spyOn(Box, 'transferPlanFromBox').mockImplementation(() =&gt; Promise.resolve());
  
        // Pulling data from MongoDB
        User.findOne({ user_name: 'testsurgeon1' }, (err, user) =&gt; {
          user.addMGSPermission();
          user.save(done);
        });
      });
  
      beforeEach(() =&gt; {
        planData2 = {
          user_name: 'hello1',
          laterality: 'right',
          plan_id: 'testplan42',
          order_number: '856-hd-02-l',
          file_id: '123456sbyuidbefui',
        };
      });
  </code></pre>
  
  <p>I hope my question isn't too vague.  Thank you for your time!</p>
  
  <p><strong>Edit 1</strong>
  I would like to point out that this code was not made by myself, but from one of our members on the software team.  He puts the object inside of the BeforeEach, and the mocks inside of the BeforeAll.  </p>
  
  <p>My confusion is that it seems like all code can be put just into BeforeAll, with a few exceptions.</p>