aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit
diff options
context:
space:
mode:
authorDave Methvin <dave.methvin@gmail.com>2015-11-23 13:57:10 -0500
committerDave Methvin <dave.methvin@gmail.com>2016-01-13 12:39:58 -0500
commit36a7cf9b1e1f96fe27710ab3f06043d01ad54d0e (patch)
tree6b6c741eaba457c55b1cf91176b8848f36673b44 /test/unit
parentbdf1b8f317d793d8ebbbe7787955edabf201a685 (diff)
downloadjquery-36a7cf9b1e1f96fe27710ab3f06043d01ad54d0e.tar.gz
jquery-36a7cf9b1e1f96fe27710ab3f06043d01ad54d0e.zip
Deferred: Warn on exceptions that are likely programming errors
Fixes gh-2736 Closes gh-2737
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/deferred.js59
1 files changed, 59 insertions, 0 deletions
diff --git a/test/unit/deferred.js b/test/unit/deferred.js
index 83c2f4f6b..d65ce34ca 100644
--- a/test/unit/deferred.js
+++ b/test/unit/deferred.js
@@ -525,6 +525,65 @@ QUnit.test( "jQuery.Deferred.then - spec compatibility", function( assert ) {
} catch ( _ ) {}
} );
+QUnit[ window.console ? "test" : "skip" ]( "jQuery.Deferred.exceptionHook", function( assert ) {
+
+ assert.expect( 1 );
+
+ var done = assert.async(),
+ defer = jQuery.Deferred(),
+ oldWarn = window.console.warn;
+
+ window.console.warn = function( msg ) {
+ assert.ok( /barf/.test( msg ), "Message: " + msg );
+ };
+ jQuery.when(
+ defer.then( function() {
+ // Should get an error
+ jQuery.barf();
+ } ).then( null, jQuery.noop ),
+ defer.then( function() {
+ // Should NOT get an error
+ throw new Error( "Make me a sandwich" );
+ } ).then( null, jQuery.noop )
+ ).then( function( ) {
+ window.console.warn = oldWarn;
+ done();
+ } );
+
+ defer.resolve();
+} );
+
+QUnit[ window.console ? "test" : "skip" ]( "jQuery.Deferred.exceptionHook with stack hooks", function( assert ) {
+
+ assert.expect( 2 );
+
+ var done = assert.async(),
+ defer = jQuery.Deferred(),
+ oldWarn = window.console.warn;
+
+ jQuery.Deferred.getStackHook = function() {
+ // Default exceptionHook assumes the stack is in a form console.warn can log,
+ // but a custom getStackHook+exceptionHook pair could save a raw form and
+ // format it to a string only when an exception actually occurs.
+ // For the unit test we just ensure the plumbing works.
+ return "NO STACK FOR YOU";
+ };
+
+ window.console.warn = function( msg, stack ) {
+ assert.ok( /cough_up_hairball/.test( msg ), "Function mentioned: " + msg );
+ assert.ok( /NO STACK FOR YOU/.test( stack ), "Stack trace included: " + stack );
+ };
+ defer.then( function() {
+ jQuery.cough_up_hairball();
+ } ).then( null, function( ) {
+ window.console.warn = oldWarn;
+ delete jQuery.Deferred.getStackHook;
+ done();
+ } );
+
+ defer.resolve();
+} );
+
QUnit.test( "jQuery.Deferred - 1.x/2.x compatibility", function( assert ) {
assert.expect( 8 );