Skip to content

Instantly share code, notes, and snippets.

@tamlyn
Last active July 7, 2022 09:48
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tamlyn/dd6d54cdbe6eccd51a4bc61f5844bec4 to your computer and use it in GitHub Desktop.
Save tamlyn/dd6d54cdbe6eccd51a4bc61f5844bec4 to your computer and use it in GitHub Desktop.
Execution order of Jest/Jasmine test code

Execution order of Jest/Jasmine test code

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.

Key points

  • Any code not inside of it, beforeAll, afterAll, beforeEach or afterEach runs immediately on initialisation.
    • This means code at the end of your file runs before even your before hooks.
    • Code inside a describe block runs even if the block or file has no active tests.
  • *All hooks wrap *Each hooks, i.e.
    • For a given test all beforeAll hooks will have run before the first beforeEach hook runs, regardless of the order in which they are defined and nested.
    • Similarly afterAll hooks run after all afterEach hooks.

How to run it

If you want to try this yourself:

  • Install Jest npm i -g jest
  • Save runOrder.test.js in a folder (ensure it is named with .test.js as a suffix)
  • Run jest in that folder
/**
* Demonstrate execution order of code in Jest/Jasmine
*/
console.log('Before describes');
describe('First test', () => {
console.log('Inside first test describe');
beforeAll(() => console.log('Before all'));
afterAll(() => console.log('After all'));
beforeEach(() => console.log('Before each'));
afterEach(() => console.log('After each'));
describe('Inner', () => {
beforeAll(() => console.log('Before all inner'));
afterAll(() => console.log('After all inner'));
beforeEach(() => console.log('Before each inner'));
afterEach(() => console.log('After each inner'));
it('runs a test', () => console.log('>>> Running first test'));
it('runs another test', () => console.log('>>> Running second test'));
});
it('runs this test too', () => console.log('>>> Running third test'));
});
console.log('Between describes');
describe('Second test', () => {
console.log('Inside second test describe');
it.skip('doesn\'t run this test', () => console.log('Nope'));
});
console.log('After describes');
$ jest
PASS ./runOrder.test.js
First test
✓ runs this test too
Inner
✓ runs a test (1ms)
✓ runs another test
Second test
○ doesn't run this test
Test Suites: 1 passed, 1 total
Tests: 1 skipped, 3 passed, 4 total
Snapshots: 0 total
Time: 0.619s, estimated 1s
Ran all test suites.
console.log runOrder.test.js:10
Before describes
console.log runOrder.test.js:14
Inside first test describe
console.log runOrder.test.js:34
Between describes
console.log runOrder.test.js:38
Inside second test describe
console.log runOrder.test.js:44
After describes
console.log runOrder.test.js:16
Before all
console.log runOrder.test.js:22
Before all inner
console.log runOrder.test.js:18
Before each
console.log runOrder.test.js:24
Before each inner
console.log runOrder.test.js:27
>>> Running first test
console.log runOrder.test.js:25
After each inner
console.log runOrder.test.js:19
After each
console.log runOrder.test.js:18
Before each
console.log runOrder.test.js:24
Before each inner
console.log runOrder.test.js:28
>>> Running second test
console.log runOrder.test.js:25
After each inner
console.log runOrder.test.js:19
After each
console.log runOrder.test.js:23
After all inner
console.log runOrder.test.js:18
Before each
console.log runOrder.test.js:31
>>> Running third test
console.log runOrder.test.js:19
After each
console.log runOrder.test.js:17
After all
@chuckplantain
Copy link

chuckplantain commented Apr 5, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment