@@ -165,26 +165,26 @@ class TestCollection extends EventEmitter {
165
165
return test ;
166
166
}
167
167
168
- _buildTestWithHooks ( test ) {
168
+ _buildTestWithHooks ( test , context ) {
169
169
if ( test . metadata . skipped || test . metadata . todo ) {
170
170
return new Sequence ( [ this . _skippedTest ( this . _buildTest ( test ) ) ] , true ) ;
171
171
}
172
172
173
- const context = { context : { } } ;
173
+ const contextRef = context ? Object . create ( context ) : { context : { } } ;
174
174
175
- const beforeHooks = this . _buildHooks ( this . hooks . beforeEach , test . title , context ) ;
176
- const afterHooks = this . _buildHooks ( this . hooks . afterEach , test . title , context ) ;
175
+ const beforeHooks = this . _buildHooks ( this . hooks . beforeEach , test . title , contextRef ) ;
176
+ const afterHooks = this . _buildHooks ( this . hooks . afterEach , test . title , contextRef ) ;
177
177
178
- let sequence = new Sequence ( [ ] . concat ( beforeHooks , this . _buildTest ( test , context ) , afterHooks ) , true ) ;
178
+ let sequence = new Sequence ( [ ] . concat ( beforeHooks , this . _buildTest ( test , contextRef ) , afterHooks ) , true ) ;
179
179
if ( this . hooks . afterEachAlways . length > 0 ) {
180
- const afterAlwaysHooks = new Sequence ( this . _buildHooks ( this . hooks . afterEachAlways , test . title , context ) ) ;
180
+ const afterAlwaysHooks = new Sequence ( this . _buildHooks ( this . hooks . afterEachAlways , test . title , contextRef ) ) ;
181
181
sequence = new Sequence ( [ sequence , afterAlwaysHooks ] , false ) ;
182
182
}
183
183
return sequence ;
184
184
}
185
185
186
- _buildTests ( tests ) {
187
- return tests . map ( test => this . _buildTestWithHooks ( test ) ) ;
186
+ _buildTests ( tests , context ) {
187
+ return tests . map ( test => this . _buildTestWithHooks ( test , context ) ) ;
188
188
}
189
189
190
190
_hasUnskippedTests ( ) {
@@ -195,18 +195,22 @@ class TestCollection extends EventEmitter {
195
195
}
196
196
197
197
build ( ) {
198
- const serialTests = new Sequence ( this . _buildTests ( this . tests . serial ) , this . bail ) ;
199
- const concurrentTests = new Concurrent ( this . _buildTests ( this . tests . concurrent ) , this . bail ) ;
200
- const allTests = new Sequence ( [ serialTests , concurrentTests ] ) ;
198
+ const sequenceTests = context => {
199
+ const serialTests = new Sequence ( this . _buildTests ( this . tests . serial , context ) , this . bail ) ;
200
+ const concurrentTests = new Concurrent ( this . _buildTests ( this . tests . concurrent , context ) , this . bail ) ;
201
+ return new Sequence ( [ serialTests , concurrentTests ] ) ;
202
+ } ;
201
203
202
204
let finalTests ;
203
205
// Only run before and after hooks when there are unskipped tests
204
206
if ( this . _hasUnskippedTests ( ) ) {
205
- const beforeHooks = new Sequence ( this . _buildHooks ( this . hooks . before ) ) ;
207
+ const context = { context : { } } ;
208
+
209
+ const beforeHooks = new Sequence ( this . _buildHooks ( this . hooks . before , null , context ) ) ;
206
210
const afterHooks = new Sequence ( this . _buildHooks ( this . hooks . after ) ) ;
207
- finalTests = new Sequence ( [ beforeHooks , allTests , afterHooks ] , true ) ;
211
+ finalTests = new Sequence ( [ beforeHooks , sequenceTests ( context ) , afterHooks ] , true ) ;
208
212
} else {
209
- finalTests = new Sequence ( [ allTests ] , true ) ;
213
+ finalTests = new Sequence ( [ sequenceTests ( ) ] , true ) ;
210
214
}
211
215
212
216
if ( this . hooks . afterAlways . length > 0 ) {
0 commit comments