My testing with Jest takeaways

Takeaways

  1. Test files (i.e suites) are run in parallel unless you use the --runInBand (in short -i) option.
  2. Tests within a file are always run sequentially (in series).
  3. A tests file is first parsed to collect tests, this is the collection phase.
  4. Tests are run in the order they were collected.
  5. describe is a good mean to organise the code BUT also influences the scope of the setup/teardown functions like beforeAll(), beforeEach(), afterAll(), afterEach()
  6. In a describe, beforeAll() and afterAll() are executed respectively before and after ALL of the tests collected from this describe block and all its nested describe blocks.
  7. In a describe, beforeEach() and afterEach() are executed respectively before and after EACH of the tests collected from this describe block and all its nested describe blocks.
  8. BEWARE, any code line inside of describe that is not part of a setup/teardown function nor a test (aka it) block, is executed during the collection phase, so avoid instructions for cleaning in there as they will be executed before the first test is run, what may not be of your intent
  9. There is ONE instance of a global variable per Test file (e.g one JSDOM instance), shared across all the tests within the tests file.
  10. Same goes with a mocked module, so make sure that you call your reset functions such as mockClear(), mockReset() either in a setup/teardown function or at top/end of each tests if you prefer that style

Reference