Skip to content

Instantly share code, notes, and snippets.

@tamlyn
Last active July 7, 2022 09:48
Show Gist options
  • Save tamlyn/dd6d54cdbe6eccd51a4bc61f5844bec4 to your computer and use it in GitHub Desktop.
Save tamlyn/dd6d54cdbe6eccd51a4bc61f5844bec4 to your computer and use it in GitHub Desktop.

Revisions

  1. tamlyn revised this gist Dec 1, 2016. 1 changed file with 12 additions and 5 deletions.
    17 changes: 12 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,8 @@
    # Execution order of Jest/Jasmine test code

    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
    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.
    - 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
  2. tamlyn revised this gist Dec 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Demonstrate execution order of code in Jest/Jasmine
    # Execution order of Jest/Jasmine test code

    How to run it:
    - Install Jest `npm i -g jest`
  3. tamlyn revised this gist Dec 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original 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 run before all `beforeEach` hooks regardless of the order in which they are defined.
    - 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.
  4. tamlyn revised this gist Dec 1, 2016. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions README.md
    Original 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.
    - 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.
    - `*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.
  5. tamlyn revised this gist Dec 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original 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.
    - 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.
  6. tamlyn revised this gist Dec 1, 2016. 1 changed file with 11 additions and 3 deletions.
    14 changes: 11 additions & 3 deletions README.md
    Original 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`
    - Put this file in a folder
    - Run `jest` in that folder
    - 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.
  7. tamlyn renamed this gist Dec 1, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. tamlyn created this gist Dec 1, 2016.
    6 changes: 6 additions & 0 deletions README.txt
    Original 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
    39 changes: 39 additions & 0 deletions runOrder.test.js
    Original 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');
    81 changes: 81 additions & 0 deletions terminal-log.txt
    Original 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