My testing with Jest takeaways
Posted on
Takeaways
- Test files (i.e suites) are run in parallel unless you use the
--runInBand
(in short-i
) option. - Tests within a file are always run sequentially (in series).
- A tests file is first parsed to collect tests, this is the collection phase.
- Tests are run in the order they were collected.
describe
is a good mean to organise the code BUT also influences the scope of the setup/teardown functions likebeforeAll()
,beforeEach()
,afterAll()
,afterEach()
- In a
describe
,beforeAll()
andafterAll()
are executed respectively before and after ALL of the tests collected from thisdescribe
block and all its nesteddescribe
blocks. - In a
describe
,beforeEach()
andafterEach()
are executed respectively before and after EACH of the tests collected from this describe block and all its nesteddescribe
blocks. - BEWARE, any code line inside of
describe
that is not part of a setup/teardown function nor a test (akait
) 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 - 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.
- 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