Core: Re-throw errors that happened in callbacks wrapped in jQuery ready

Also, expose jQuery.readyException that allows to overwrite the default
ready error handler.

Fixes gh-3174
Closes gh-3210
This commit is contained in:
Michał Gołębiowski
2016-06-29 14:19:04 +02:00
parent 25d8ccd111
commit ad6a94c3f1
3 changed files with 74 additions and 2 deletions

View File

@@ -1,4 +1,12 @@
QUnit.module( "core", { teardown: moduleTeardown } );
QUnit.module( "core", {
setup: function() {
this.sandbox = sinon.sandbox.create();
},
teardown: function() {
this.sandbox.restore();
return moduleTeardown.apply( this, arguments );
}
} );
QUnit.test( "Basic requirements", function( assert ) {
assert.expect( 7 );
@@ -1709,3 +1717,45 @@ QUnit.test( "Iterability of jQuery objects (gh-1693)", function( assert ) {
assert.ok( true, "The browser doesn't support Symbols" );
}
} );
QUnit[ jQuery.Deferred ? "test" : "skip" ]( "jQuery.readyException (original)", function( assert ) {
assert.expect( 1 );
var message;
this.sandbox.stub( window, "setTimeout", function( fn ) {
try {
fn();
} catch ( error ) {
message = error.message;
}
} );
jQuery( function() {
throw new Error( "Error in jQuery ready" );
} );
assert.strictEqual(
message,
"Error in jQuery ready",
"The error should have been thrown in a timeout"
);
} );
QUnit[ jQuery.Deferred ? "test" : "skip" ]( "jQuery.readyException (custom)", function( assert ) {
assert.expect( 1 );
var done = assert.async();
this.sandbox.stub( jQuery, "readyException", function( error ) {
assert.strictEqual(
error.message,
"Error in jQuery ready",
"The custom jQuery.readyException should have been called"
);
done();
} );
jQuery( function() {
throw new Error( "Error in jQuery ready" );
} );
} );