| 1 |
/** |
| 2 |
* Copyright 2013 The Closure Library Authors. All Rights Reserved. |
| 3 |
* Author: [email protected] (Malte Ubl) |
| 4 |
*/ |
| 5 |
|
| 6 |
goog.setTestOnly(); |
| 7 |
goog.require('goog.async.Deferred'); |
| 8 |
goog.require('goog.testing.jsunit'); |
| 9 |
|
| 10 |
function shouldRunTests() { |
| 11 |
return !!Error.captureStackTrace; |
| 12 |
} |
| 13 |
|
| 14 |
function testErrorStack() { |
| 15 |
var d; |
| 16 |
// Get the deferred from somewhere deep in the callstack. |
| 17 |
(function immediate() { |
| 18 |
(function immediate2() { |
| 19 |
d = new goog.async.Deferred(); |
| 20 |
d.addCallback(function actuallyThrows() { throw new Error('Foo'); }); |
| 21 |
})(); |
| 22 |
})(); |
| 23 |
|
| 24 |
window.setTimeout(function willThrow() { |
| 25 |
(function callbackCaller() { d.callback(); })(); |
| 26 |
}, 0); |
| 27 |
|
| 28 |
return d.then(fail, function(error) { |
| 29 |
assertContains('Foo', error.stack); |
| 30 |
assertContains('testErrorStack', error.stack); |
| 31 |
assertContains('callbackCaller', error.stack); |
| 32 |
assertContains('willThrow', error.stack); |
| 33 |
assertContains('actuallyThrows', error.stack); |
| 34 |
assertContains('DEFERRED OPERATION', error.stack); |
| 35 |
assertContains('immediate', error.stack); |
| 36 |
assertContains('immediate2', error.stack); |
| 37 |
}); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
function testErrorStack_forErrback() { |
| 42 |
var d = new goog.async.Deferred(); |
| 43 |
|
| 44 |
window.setTimeout(function willThrow() { d.errback(new Error('Foo')); }, 0); |
| 45 |
|
| 46 |
return d.then(fail, function(error) { |
| 47 |
assertContains('Foo', error.stack); |
| 48 |
assertContains('testErrorStack_forErrback', error.stack); |
| 49 |
assertContains('willThrow', error.stack); |
| 50 |
assertContains('DEFERRED OPERATION', error.stack); |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
function testErrorStack_nested() { |
| 56 |
var d = new goog.async.Deferred(); |
| 57 |
|
| 58 |
window.setTimeout(function async1() { |
| 59 |
var nested = new goog.async.Deferred(); |
| 60 |
nested.addErrback(function nestedErrback(error) { d.errback(error); }); |
| 61 |
window.setTimeout(function async2() { |
| 62 |
(function immediate() { nested.errback(new Error('Foo')); })(); |
| 63 |
}); |
| 64 |
}, 0); |
| 65 |
|
| 66 |
return d.then(fail, function(error) { |
| 67 |
assertContains('Foo', error.stack); |
| 68 |
assertContains('testErrorStack_nested', error.stack); |
| 69 |
assertContains('async1', error.stack); |
| 70 |
assertContains('async2', error.stack); |
| 71 |
assertContains('immediate', error.stack); |
| 72 |
assertContains('DEFERRED OPERATION', error.stack); |
| 73 |
}); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
function testErrorStack_doesNotTouchCustomStack() { |
| 78 |
var d = new goog.async.Deferred(); |
| 79 |
d.addCallback(function actuallyThrows() { |
| 80 |
var e = new Error('Foo'); |
| 81 |
e.stack = 'STACK'; |
| 82 |
throw e; |
| 83 |
}); |
| 84 |
|
| 85 |
window.setTimeout(function willThrow() { |
| 86 |
(function callbackCaller() { d.callback(); })(); |
| 87 |
}, 0); |
| 88 |
|
| 89 |
return d.then(fail, function(error) { assertEquals('STACK', error.stack); }); |
| 90 |
} |