-
Notifications
You must be signed in to change notification settings - Fork 781
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core: New
error
event for bailing on uncaught errors
- Loading branch information
Showing
25 changed files
with
354 additions
and
291 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,51 @@ | ||
import config from "./config"; | ||
import ProcessingQueue from "./processing-queue"; | ||
import { globalSuite } from "../core"; | ||
import { sourceFromStacktrace } from "./stacktrace"; | ||
import { extend, errorString } from "./utilities"; | ||
import Logger from "../logger"; | ||
import { test } from "../test"; | ||
import { errorString } from "./utilities"; | ||
import { emit } from "../events"; | ||
|
||
/** | ||
* Handle a global error that should result in a failed test run. | ||
* | ||
* Summary: | ||
* | ||
* - If there is a current test, it becomes a failed assertion. | ||
* - If there is a current module, it becomes a failed test (and bypassing filters). | ||
* Note that if we're before any other test or module, it will naturally | ||
* become a global test. | ||
* - If the overall test run has ended, the error is printed to `console.warn()`. | ||
* - If we're strictly inside a test (or one if its module hooks), the exception | ||
* becomes a failed assertion. | ||
* | ||
* This has the important side-effect that uncaught exceptions (such as | ||
* calling an undefined function) during a "todo" test do NOT result in | ||
* a failed test run. | ||
* | ||
* - If we're anywhere outside a test (be it in early event callbacks, or | ||
* internally between tests, or somewhere after "runEnd" if the process is | ||
* still alive for some reason), then send an "error" event to the reporters. | ||
* | ||
* @since 2.17.0 | ||
* @param {Error|any} error | ||
*/ | ||
export default function onUncaughtException( error ) { | ||
const message = errorString( error ); | ||
|
||
// We could let callers specify an extra offset to add to the number passed to | ||
// sourceFromStacktrace, in case they are a wrapper further away from the error | ||
// handler, and thus reduce some noise in the stack trace. However, we're not | ||
// doing this right now because it would almost never be used in practice given | ||
// the vast majority of error values will be an Error object, and thus have a | ||
// clean stack trace already. | ||
const source = error.stack || sourceFromStacktrace( 2 ); | ||
|
||
if ( config.current ) { | ||
config.current.assert.pushResult( { | ||
result: false, | ||
message: `global failure: ${message}`, | ||
source | ||
message: `global failure: ${errorString( error )}`, | ||
|
||
// We could let callers specify an offset to subtract a number of frames via | ||
// sourceFromStacktrace, in case they are a wrapper further away from the error | ||
// handler, and thus reduce some noise in the stack trace. However, we're not | ||
// doing this right now because it would almost never be used in practice given | ||
// the vast majority of error values will be Error objects, and thus have their | ||
// own stack trace already. | ||
source: ( error && error.stack ) || sourceFromStacktrace( 2 ) | ||
} ); | ||
} else if ( !ProcessingQueue.finished ) { | ||
test( "global failure", extend( function( assert ) { | ||
assert.pushResult( { result: false, message, source } ); | ||
}, { validTest: true } ) ); | ||
} else { | ||
|
||
// TODO: Once supported in js-reporters and QUnit, use a TAP "bail" event. | ||
// The CLI runner can use this to ensure a non-zero exit code, even if | ||
// emitted after "runEnd" and before the process exits. | ||
// The HTML Reporter can use this to renmder it on the page in a test-like | ||
// block for easy discovery. | ||
// | ||
// Avoid printing "Error: foo" twice if the environment's native stack trace | ||
// already includes that in its format. | ||
Logger.warn( source.indexOf( source ) !== -1 ? source : `${message}\n${source}` ); | ||
// The "error" event was added in QUnit 2.17. | ||
// Increase "bad assertion" stats despite no longer pushing an assertion in this case. | ||
// This ensures "runEnd" and "QUnit.done()" handlers behave as expected, since the "bad" | ||
// count is typically how reporters decide on the boolean outcome of the test run. | ||
globalSuite.globalFailureCount++; | ||
config.stats.bad++; | ||
config.stats.all++; | ||
emit( "error", error ); | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.