@@ -24,6 +24,7 @@ var escapeRe = require('escape-string-regexp');
2424module . exports = function ( suite ) {
2525 var suites = [ suite ] ;
2626 suite . timeout ( 0 ) ;
27+ var afterAllHooks , afterEachHooks , afterAllHooksAreLoaded , afterEachHooksAreLoaded ;
2728
2829 suite . on ( 'pre-require' , function ( context , file , mocha ) {
2930 var common = require ( 'mocha/lib/interfaces/common' ) ( suites , context ) ;
@@ -45,6 +46,12 @@ module.exports = function (suite) {
4546 suites . shift ( ) ;
4647 }
4748 if ( ! opts ) opts = { } ;
49+
50+ afterAllHooks = [ ] ;
51+ afterEachHooks = [ ] ;
52+ afterAllHooksAreLoaded = false ;
53+ afterEachHooksAreLoaded = false ;
54+
4855 var suite = Suite . create ( suites [ 0 ] , title ) ;
4956 suite . timeout ( 0 ) ;
5057
@@ -54,28 +61,28 @@ module.exports = function (suite) {
5461 suite . file = file ;
5562 suites . unshift ( suite ) ;
5663 suite . beforeEach ( 'codeceptjs.before' , scenario . setup ) ;
57- suite . afterEach ( 'finialize codeceptjs' , scenario . teardown ) ;
64+ afterEachHooks . push ( [ 'finialize codeceptjs' , scenario . teardown ] ) ;
5865
5966 suite . beforeAll ( 'codeceptjs.beforeSuite' , ( ) => scenario . suiteSetup ( suite ) ) ;
60- suite . afterAll ( 'codeceptjs.afterSuite' , ( ) => scenario . suiteTeardown ( suite ) ) ;
67+ afterAllHooks . push ( [ 'codeceptjs.afterSuite' , ( ) => scenario . suiteTeardown ( suite ) ] ) ;
6168
6269 return suite ;
6370 } ;
6471
6572 context . BeforeSuite = function ( fn ) {
66- suites [ 0 ] . beforeAll ( 'BeforeSuite' , scenario . injected ( fn , suites [ 0 ] ) ) ;
73+ suites [ 0 ] . beforeAll ( 'BeforeSuite' , scenario . injected ( fn , suites [ 0 ] , 'beforeSuite' ) ) ;
6774 } ;
6875
6976 context . AfterSuite = function ( fn ) {
70- suites [ 0 ] . afterAll ( 'AfterSuite' , scenario . injected ( fn , suites [ 0 ] ) ) ;
77+ afterAllHooks . unshift ( [ 'AfterSuite' , scenario . injected ( fn , suites [ 0 ] , 'afterSuite' ) ] ) ;
7178 } ;
7279
7380 context . Background = context . Before = function ( fn ) {
74- suites [ 0 ] . beforeEach ( 'Before' , scenario . injected ( fn , suites [ 0 ] ) ) ;
81+ suites [ 0 ] . beforeEach ( 'Before' , scenario . injected ( fn , suites [ 0 ] , 'before' ) ) ;
7582 } ;
7683
7784 context . After = function ( fn ) {
78- suites [ 0 ] . afterEach ( 'After' , scenario . injected ( fn , suites [ 0 ] ) ) ;
85+ afterEachHooks . unshift ( [ 'After' , scenario . injected ( fn , suites [ 0 ] ) , 'after' ] ) ;
7986 } ;
8087
8188 /**
@@ -88,7 +95,22 @@ module.exports = function (suite) {
8895 fn = opts ;
8996 opts = { } ;
9097 }
91-
98+ /**
99+ * load hooks from arrays to suite to prevent reordering
100+ *
101+ */
102+ if ( ! afterAllHooksAreLoaded ) {
103+ afterAllHooks . forEach ( function ( hook ) {
104+ suites [ 0 ] . afterAll ( hook [ 0 ] , hook [ 1 ] ) ;
105+ } ) ;
106+ afterAllHooksAreLoaded = true ;
107+ }
108+ if ( ! afterEachHooksAreLoaded ) {
109+ afterEachHooks . forEach ( function ( hook ) {
110+ suites [ 0 ] . afterEach ( hook [ 0 ] , hook [ 1 ] ) ;
111+ } ) ;
112+ afterEachHooksAreLoaded = true ;
113+ }
92114 var suite = suites [ 0 ] ;
93115 if ( suite . pending ) {
94116 fn = null ;
0 commit comments