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