Skip to content

Commit fd87b59

Browse files
committed
Adds proper unit test for exceptions thrown from within a callback.
1 parent 1be59ef commit fd87b59

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

test/unit.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ testJSONP( "error (HTTP Code)", "error", {
4444
}
4545
});
4646

47-
if ( window.opera && window.opera.version() < 11.60 ) {
47+
if ( !window.opera || window.opera.version() < 11.60 ) {
4848
testJSONP( "error (Syntax Error)", "error", {
4949
expect: window.opera ? 0 : 1,
5050
url: "data/syntax-error.js",
@@ -61,6 +61,31 @@ if ( window.opera && window.opera.version() < 11.60 ) {
6161
});
6262
}
6363

64+
if ( !window.opera ) {
65+
test( "error (Exception)", function() {
66+
stop();
67+
expect( 2 );
68+
$.jsonp({
69+
url: "http://gdata.youtube.com/feeds/api/users/julianaubourg?callback=?",
70+
beforeSend: function() {
71+
var oldOnError = window.onerror;
72+
window.onerror = function( flag ) {
73+
ok( flag !== undefined, "Exception Thrown" );
74+
window.onerror = oldOnError;
75+
start();
76+
};
77+
},
78+
success: function() {
79+
ok( true, "success" );
80+
throw "an exception";
81+
},
82+
complete: function() {
83+
window.onerror();
84+
}
85+
});
86+
});
87+
}
88+
6489
testJSONP( "error (callback not called)", "error", {
6590
expect: 1,
6691
url: "data/no-callback.js",
@@ -96,26 +121,3 @@ test( "stress test", function() {
96121
});
97122
}
98123
});
99-
100-
101-
test( "callback error", function() {
102-
expect(2);
103-
104-
$.jsonp({
105-
url: "http://gdata.youtube.com/feeds/api/users/julianaubourg?callback=?",
106-
cache: true,
107-
success: function() {
108-
try {
109-
return eval("var j = 7 + // syntax error");
110-
}
111-
catch(e) {
112-
ok( true, "Syntax error thrown");
113-
}
114-
},
115-
complete: function() {
116-
start();
117-
}
118-
});
119-
stop();
120-
ok(true, "done"); // to resume tests
121-
});

0 commit comments

Comments
 (0)