While tests run in source order, surrounding code does not which can lead to hard to debug issues.
Compare the test file below with the sample output below that and note the order of the log messages.
- Any code not inside of
it,beforeAll,afterAll,beforeEachorafterEachruns immediately on initialisation.- This means code at the end of your file runs before even your before hooks.
- Code inside a
describeblock runs even if the block or file has no active tests.
*Allhooks wrap*Eachhooks, i.e.- For a given test all
beforeAllhooks will have run before the firstbeforeEachhook runs, regardless of the order in which they are defined and nested. - Similarly
afterAllhooks run after allafterEachhooks.
- For a given test all
If you want to try this yourself:
- Install Jest
npm i -g jest - Save
runOrder.test.jsin a folder (ensure it is named with.test.jsas a suffix) - Run
jestin that folder
async version of above, fwiw: https://gist.github.com/chuckplantain/8359063a1c025b5ccfce52b4f5707368