-
Notifications
You must be signed in to change notification settings - Fork 781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Core: New error
event for bailing on uncaught errors
#1638
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 substract a number of frames via | ||
Krinkle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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 ); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import Logger from "../logger"; | ||
import config from "./config"; | ||
import onUncaughtException from "./on-uncaught-exception"; | ||
|
||
|
@@ -13,6 +14,7 @@ import onUncaughtException from "./on-uncaught-exception"; | |
* to receive an "expected" error during `assert.throws()`. | ||
* | ||
* @see <https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror> | ||
* @deprecated since 2.17.0 | ||
* @param {Object} details | ||
* @param {string} details.message | ||
* @param {string} details.fileName | ||
|
@@ -21,6 +23,8 @@ import onUncaughtException from "./on-uncaught-exception"; | |
* @return {bool} True if native error reporting should be suppressed. | ||
*/ | ||
export default function onWindowError( details ) { | ||
Logger.warn( "QUnit.onError is deprecated and will be removed in QUnit 3.0." ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a suggested fix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I did not provide a direct replacement. This was an internal method in my mind, with a rather awkward/incompete responsibility. It wasn't exactly a As part of 07de3c6, I factored out the part I think is re-usable, into If someone out there did end up using this, as part of a custom HTML Reporter or something, and consumed this method, then a replacement would involve calling Speaking of migration guide, I've made a note of that at #1498. TODO: Improve console warning message. |
||
|
||
if ( config.current && config.current.ignoreGlobalErrors ) { | ||
return true; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import QUnit from "../core"; | ||
import Test from "../test"; | ||
import { extractStacktrace } from "../core/stacktrace"; | ||
import { now, extend } from "../core/utilities"; | ||
import { now, extend, errorString } from "../core/utilities"; | ||
import { window, document, navigator, console } from "../globals"; | ||
import "./urlparams"; | ||
import fuzzysort from "fuzzysort"; | ||
|
||
// TODO: Remove adhoc counting in favour of stats from QUnit.done() or "runEnd" event. | ||
const stats = { | ||
passedTests: 0, | ||
failedTests: 0, | ||
|
@@ -654,21 +654,27 @@ export function escapeText( s ) { | |
title = document.createElement( "strong" ); | ||
title.innerHTML = getNameHtml( name, moduleName ); | ||
|
||
rerunTrigger = document.createElement( "a" ); | ||
rerunTrigger.innerHTML = "Rerun"; | ||
rerunTrigger.href = setUrl( { testId: testId } ); | ||
|
||
testBlock = document.createElement( "li" ); | ||
testBlock.appendChild( title ); | ||
testBlock.appendChild( rerunTrigger ); | ||
testBlock.id = "qunit-test-output-" + testId; | ||
|
||
// No ID or rerun link for "global failure" blocks | ||
if ( testId !== undefined ) { | ||
rerunTrigger = document.createElement( "a" ); | ||
rerunTrigger.innerHTML = "Rerun"; | ||
rerunTrigger.href = setUrl( { testId: testId } ); | ||
|
||
testBlock.id = "qunit-test-output-" + testId; | ||
testBlock.appendChild( rerunTrigger ); | ||
} | ||
|
||
assertList = document.createElement( "ol" ); | ||
assertList.className = "qunit-assert-list"; | ||
|
||
testBlock.appendChild( assertList ); | ||
|
||
tests.appendChild( testBlock ); | ||
|
||
return testBlock; | ||
} | ||
|
||
// HTML Reporter initialization and load | ||
|
@@ -1027,6 +1033,34 @@ export function escapeText( s ) { | |
} | ||
} ); | ||
|
||
QUnit.on( "error", ( error ) => { | ||
stats.failedTests++; | ||
|
||
const testItem = appendTest( "global failure" ); | ||
if ( !testItem ) { | ||
|
||
// HTML Reporter is probably disabled or not yet initialized. | ||
console.warn( "global failure" ); | ||
console.warn( error ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warn vs error? I'm good with either, but maybe I was surprised to find this wasn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it's an awkward fit, though note that we did also use This was a compromise to keep the diff minimal because we don't actually interface yet with |
||
return; | ||
} | ||
|
||
// Render similar to a failed assertion (see above QUnit.log callback) | ||
let message = escapeText( errorString( error ) ); | ||
message = "<span class='test-message'>" + message + "</span>"; | ||
if ( error && error.stack ) { | ||
message += "<table>" + | ||
"<tr class='test-source'><th>Source: </th><td><pre>" + | ||
escapeText( error.stack ) + "</pre></td></tr>" + | ||
"</table>"; | ||
} | ||
const assertList = testItem.getElementsByTagName( "ol" )[ 0 ]; | ||
const assertLi = document.createElement( "li" ); | ||
assertLi.className = "fail"; | ||
assertLi.innerHTML = message; | ||
assertList.appendChild( assertLi ); | ||
} ); | ||
|
||
// Avoid readyState issue with phantomjs | ||
// Ref: #818 | ||
var usingPhantom = ( function( p ) { | ||
|
@@ -1068,21 +1102,27 @@ export function escapeText( s ) { | |
// Treat return value as window.onerror itself does, | ||
// Only do our handling if not suppressed. | ||
if ( ret !== true ) { | ||
const error = { | ||
message, | ||
fileName, | ||
lineNumber | ||
}; | ||
|
||
// If there is a current test that sets the internal `ignoreGlobalErrors` field | ||
// (such as during `assert.throws()`), then the error is ignored and native | ||
// error reporting is suppressed as well. This is because in browsers, an error | ||
// can sometimes end up in `window.onerror` instead of in the local try/catch. | ||
// This ignoring of errors does not apply to our general onUncaughtException | ||
// method, nor to our `unhandledRejection` handlers, as those are not meant | ||
// to receive an "expected" error during `assert.throws()`. | ||
if ( config.current && config.current.ignoreGlobalErrors ) { | ||
return true; | ||
} | ||
|
||
// According to | ||
// https://blog.sentry.io/2016/01/04/client-javascript-reporting-window-onerror, | ||
// most modern browsers support an errorObj argument; use that to | ||
// get a full stack trace if it's available. | ||
if ( errorObj && errorObj.stack ) { | ||
error.stacktrace = extractStacktrace( errorObj, 0 ); | ||
const error = errorObj || new Error( message ); | ||
if ( !error.stack && fileName && lineNumber ) { | ||
error.stack = `${fileName}:${lineNumber}`; | ||
} | ||
|
||
ret = QUnit.onError( error ); | ||
QUnit.onUncaughtException( error ); | ||
} | ||
|
||
return ret; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uncaught errors now increment these. As errors can happen before/during
begin()
, I had to move this toconfig.js
as the increment would fail otherwise with the stats object being undefined. We can move more of this over, but that's for another day.