Last active
July 7, 2022 09:48
-
-
Save tamlyn/dd6d54cdbe6eccd51a4bc61f5844bec4 to your computer and use it in GitHub Desktop.
Revisions
-
tamlyn revised this gist
Dec 1, 2016 . 1 changed file with 12 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,8 @@ # 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 @@ -12,4 +11,12 @@ How to run it: - 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 -
tamlyn revised this gist
Dec 1, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Execution order of Jest/Jasmine test code How to run it: - Install Jest `npm i -g jest` -
tamlyn revised this gist
Dec 1, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,5 +11,5 @@ How to run it: - 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. -
tamlyn revised this gist
Dec 1, 2016 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,5 +10,6 @@ How to run it: - 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 run before all `beforeEach` hooks regardless of the order in which they are defined. - Similarly `afterAll` hooks run after all `afterEach` hooks. -
tamlyn revised this gist
Dec 1, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ How to run it: ## 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. - Nested `beforeAll` hooks run before all `beforeEach` hooks regardless of the order in which they are defined. -
tamlyn revised this gist
Dec 1, 2016 . 1 changed file with 11 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,14 @@ # Demonstrate execution order of code in Jest/Jasmine How to run it: - 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 ## 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. - Nested `beforeAll` hooks run before all `beforeEach` hooks regardless of the order in which they are defined. - Similarly `afterAll` hooks run after all `afterEach` hooks. -
tamlyn renamed this gist
Dec 1, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
tamlyn created this gist
Dec 1, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ # Demonstrate execution order of code in Jest/Jasmine How to run it: - Install Jest `npm i -g jest` - Put this file in a folder - Run `jest` in that folder This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ /** * 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'); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ $ 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