Created
April 20, 2016 17:33
-
-
Save harto/c97d2fc9d0bfaf20706eb2acbf48c908 to your computer and use it in GitHub Desktop.
Mocha before() & beforeEach() execution order with nested describe()
This file contains 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 characters
'use strict'; | |
describe('mocha before hooks', function () { | |
before(() => console.log('*** top-level before()')); | |
beforeEach(() => console.log('*** top-level beforeEach()')); | |
describe('nesting', function () { | |
before(() => console.log('*** nested before()')); | |
beforeEach(() => console.log('*** nested beforeEach()')); | |
it('is a nested spec', () => true); | |
}); | |
}); | |
// mocha before hooks | |
// *** top-level before() | |
// nesting | |
// *** nested before() | |
// *** top-level beforeEach() | |
// *** nested beforeEach() | |
// ✓ is a nested spec | |
// | |
// | |
// 1 passing (8ms) |
this was very helpful!
Thank you!
top level
beforeEach
initializes something that is used by the nestedbefore
From the example above the nested before()
is called before the top level beforeEach()
.
So if the nested before()
relies on the top level beforeEach()
to initialize something, the result might be undefined
or null
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this. I was trying to access a nested object property within the nested
describe
block and getting a TypeError because thebeforeEach
hook in the top-leveldescribe
, where the object was being assigned, was not being called prior to that so the object was in fact undefined.before
after