Skip to content

Instantly share code, notes, and snippets.

@yoavniran
Created April 11, 2015 13:35
Show Gist options
  • Save yoavniran/ad29e7ecfe57570b18f7 to your computer and use it in GitHub Desktop.
Save yoavniran/ad29e7ecfe57570b18f7 to your computer and use it in GitHub Desktop.

Revisions

  1. yoavniran created this gist Apr 11, 2015.
    106 changes: 106 additions & 0 deletions mocha-hooks-order.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,106 @@
    describe("root context", function(){

    before(function(){
    console.log("before: root");
    });

    beforeEach(function(){
    console.log("beforeEach: root");
    });

    it("test 1", function(){

    });

    describe("first context", function(){

    before(function(){
    console.log("\tbefore: first");
    });

    beforeEach(function(){
    console.log("\tbeforeEach: first");
    });

    it("test first.1", function(){});

    it("test first.2", function(){});


    after(function(){
    console.log("\tafter: first");
    });

    afterEach(function(){
    console.log("\tafterEach: first");
    });
    });

    describe("second context", function(){

    before(function(){
    console.log("\t\tbefore: second");
    });

    beforeEach(function(){
    console.log("\t\tbeforeEach: second");
    });

    it("test second.1", function(){});

    it("test second.2", function(){});

    after(function(){
    console.log("\t\tafter: second");
    });

    afterEach(function(){
    console.log("\t\tafterEach: second");
    });
    });

    after(function(){
    console.log("after: root");
    });

    afterEach(function(){
    console.log("afterEach: root");
    });
    });

    //results in:

    //root context
    //before: root
    //beforeEach: root
    //✓ test 1
    //afterEach: root
    //first context
    //before: first
    //beforeEach: root
    //beforeEach: first
    //✓ test first.1
    //afterEach: first
    //afterEach: root
    //beforeEach: root
    //beforeEach: first
    //✓ test first.2
    //afterEach: first
    //afterEach: root
    //after: first
    //second context
    //before: second
    //beforeEach: root
    //beforeEach: second
    //✓ test second.1
    //afterEach: second
    //afterEach: root
    //beforeEach: root
    //beforeEach: second
    //✓ test second.2
    //afterEach: second
    //afterEach: root
    //after: second
    //after: root