@@ -164,28 +164,41 @@ export class WorkerMain extends ProcessRunner {
164
164
}
165
165
166
166
unhandledError ( error : Error | any ) {
167
+ const failWithFatalErrorAndStop = ( ) => {
168
+ if ( ! this . _fatalErrors . length )
169
+ this . _fatalErrors . push ( serializeError ( error ) ) ;
170
+ void this . _stop ( ) ;
171
+ } ;
172
+
173
+ // No current test - fatal error.
174
+ if ( ! this . _currentTest )
175
+ return failWithFatalErrorAndStop ( ) ;
176
+
167
177
// Usually, we do not differentiate between errors in the control flow
168
178
// and unhandled errors - both lead to the test failing. This is good for regular tests,
169
179
// so that you can, e.g. expect() from inside an event handler. The test fails,
170
180
// and we restart the worker.
171
- //
181
+ if ( this . _currentTest . expectedStatus !== 'failed' ) {
182
+ this . _currentTest . _failWithError ( serializeError ( error ) , true /* isHardError */ ) ;
183
+ void this . _stop ( ) ;
184
+ return ;
185
+ }
186
+
172
187
// However, for tests marked with test.fail(), this is a problem. Unhandled error
173
188
// could come either from the user test code (legit failure), or from a fixture or
174
189
// a test runner. In the latter case, the worker state could be messed up,
175
190
// and continuing to run tests in the same worker is problematic. Therefore,
176
191
// we turn this into a fatal error and restart the worker anyway.
192
+ //
177
193
// The only exception is the expect() error that we still consider ok.
178
194
const isExpectError = ( error instanceof Error ) && ! ! ( error as any ) . matcherResult ;
179
- const isCurrentTestExpectedToFail = this . _currentTest ?. expectedStatus === 'failed' ;
180
- const shouldConsiderAsTestError = isExpectError || ! isCurrentTestExpectedToFail ;
181
- if ( this . _currentTest && shouldConsiderAsTestError ) {
195
+ if ( isExpectError ) {
196
+ // Note: do not stop the worker, because test marked with test.fail() that fails an assertion
197
+ // is perfectly fine.
182
198
this . _currentTest . _failWithError ( serializeError ( error ) , true /* isHardError */ ) ;
183
199
} else {
184
- // No current test - fatal error.
185
- if ( ! this . _fatalErrors . length )
186
- this . _fatalErrors . push ( serializeError ( error ) ) ;
200
+ failWithFatalErrorAndStop ( ) ;
187
201
}
188
- void this . _stop ( ) ;
189
202
}
190
203
191
204
private async _loadIfNeeded ( ) {
0 commit comments