Skip to content

Instantly share code, notes, and snippets.

@yoavniran
Created April 11, 2015 13:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoavniran/ad29e7ecfe57570b18f7 to your computer and use it in GitHub Desktop.
Save yoavniran/ad29e7ecfe57570b18f7 to your computer and use it in GitHub Desktop.
shows how mocha hooks are ordered and run within contexts
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment