|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | import {zoneSymbol} from '../../lib/common/utils';
|
| 10 | +import {ifEnvSupports, isSupportSetErrorStack} from '../test-util'; |
| 11 | + |
10 | 12 | const defineProperty = (Object as any)[zoneSymbol('defineProperty')] || Object.defineProperty;
|
11 | 13 |
|
12 |
| -describe('longStackTraceZone', function() { |
13 |
| - let log: Error[]; |
14 |
| - let lstz: Zone; |
15 |
| - let longStackTraceZoneSpec = (Zone as any)['longStackTraceZoneSpec']; |
| 14 | +describe( |
| 15 | + 'longStackTraceZone', ifEnvSupports(isSupportSetErrorStack, function() { |
| 16 | + let log: Error[]; |
| 17 | + let lstz: Zone; |
| 18 | + let longStackTraceZoneSpec = (Zone as any)['longStackTraceZoneSpec']; |
16 | 19 |
|
17 |
| - beforeEach(function() { |
18 |
| - lstz = Zone.current.fork(longStackTraceZoneSpec).fork({ |
19 |
| - name: 'long-stack-trace-zone-test', |
20 |
| - onHandleError: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, |
21 |
| - error: any): boolean => { |
22 |
| - parentZoneDelegate.handleError(targetZone, error); |
23 |
| - log.push(error); |
24 |
| - return false; |
25 |
| - } |
26 |
| - }); |
| 20 | + beforeEach(function() { |
| 21 | + lstz = Zone.current.fork(longStackTraceZoneSpec).fork({ |
| 22 | + name: 'long-stack-trace-zone-test', |
| 23 | + onHandleError: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, |
| 24 | + error: any): boolean => { |
| 25 | + parentZoneDelegate.handleError(targetZone, error); |
| 26 | + log.push(error); |
| 27 | + return false; |
| 28 | + } |
| 29 | + }); |
27 | 30 |
|
28 |
| - log = []; |
29 |
| - }); |
| 31 | + log = []; |
| 32 | + }); |
30 | 33 |
|
31 |
| - function expectElapsed(stack: string, expectedCount: number) { |
32 |
| - try { |
33 |
| - let actualCount = stack.split('_Elapsed_').length; |
34 |
| - if (actualCount !== expectedCount) { |
35 |
| - expect(actualCount).toEqual(expectedCount); |
36 |
| - console.log(stack); |
| 34 | + function expectElapsed(stack: string, expectedCount: number) { |
| 35 | + try { |
| 36 | + let actualCount = stack.split('_Elapsed_').length; |
| 37 | + if (actualCount !== expectedCount) { |
| 38 | + expect(actualCount).toEqual(expectedCount); |
| 39 | + console.log(stack); |
| 40 | + } |
| 41 | + } catch (e) { |
| 42 | + expect(e).toBe(null); |
| 43 | + } |
37 | 44 | }
|
38 |
| - } catch (e) { |
39 |
| - expect(e).toBe(null); |
40 |
| - } |
41 |
| - } |
42 | 45 |
|
43 |
| - it('should produce long stack traces', function(done) { |
44 |
| - lstz.run(function() { |
45 |
| - setTimeout(function() { |
46 |
| - setTimeout(function() { |
| 46 | + it('should produce long stack traces', function(done) { |
| 47 | + lstz.run(function() { |
47 | 48 | setTimeout(function() {
|
48 |
| - expectElapsed(log[0].stack, 3); |
49 |
| - done(); |
| 49 | + setTimeout(function() { |
| 50 | + setTimeout(function() { |
| 51 | + expectElapsed(log[0].stack, 3); |
| 52 | + done(); |
| 53 | + }, 0); |
| 54 | + throw new Error('Hello'); |
| 55 | + }, 0); |
50 | 56 | }, 0);
|
51 |
| - throw new Error('Hello'); |
52 |
| - }, 0); |
53 |
| - }, 0); |
54 |
| - }); |
55 |
| - }); |
| 57 | + }); |
| 58 | + }); |
56 | 59 |
|
57 |
| - it('should produce a long stack trace even if stack setter throws', (done) => { |
58 |
| - let wasStackAssigned = false; |
59 |
| - let error = new Error('Expected error'); |
60 |
| - defineProperty(error, 'stack', { |
61 |
| - configurable: false, |
62 |
| - get: () => 'someStackTrace', |
63 |
| - set: (v: any) => { |
64 |
| - throw new Error('no writes'); |
65 |
| - } |
66 |
| - }); |
67 |
| - lstz.run(() => { |
68 |
| - setTimeout(() => { |
69 |
| - throw error; |
| 60 | + it('should produce a long stack trace even if stack setter throws', (done) => { |
| 61 | + let wasStackAssigned = false; |
| 62 | + let error = new Error('Expected error'); |
| 63 | + defineProperty(error, 'stack', { |
| 64 | + configurable: false, |
| 65 | + get: () => 'someStackTrace', |
| 66 | + set: (v: any) => { |
| 67 | + throw new Error('no writes'); |
| 68 | + } |
| 69 | + }); |
| 70 | + lstz.run(() => { |
| 71 | + setTimeout(() => { |
| 72 | + throw error; |
| 73 | + }); |
| 74 | + }); |
| 75 | + setTimeout(() => { |
| 76 | + const e = log[0]; |
| 77 | + expect((e as any).longStack).toBeTruthy(); |
| 78 | + done(); |
| 79 | + }); |
70 | 80 | });
|
71 |
| - }); |
72 |
| - setTimeout(() => { |
73 |
| - const e = log[0]; |
74 |
| - expect((e as any).longStack).toBeTruthy(); |
75 |
| - done(); |
76 |
| - }); |
77 |
| - }); |
78 | 81 |
|
79 |
| - it('should produce long stack traces when has uncaught error in promise', function(done) { |
80 |
| - lstz.runGuarded(function() { |
81 |
| - setTimeout(function() { |
82 |
| - setTimeout(function() { |
83 |
| - let promise = new Promise(function(resolve, reject) { |
| 82 | + it('should produce long stack traces when has uncaught error in promise', function(done) { |
| 83 | + lstz.runGuarded(function() { |
| 84 | + setTimeout(function() { |
84 | 85 | setTimeout(function() {
|
85 |
| - reject(new Error('Hello Promise')); |
| 86 | + let promise = new Promise(function(resolve, reject) { |
| 87 | + setTimeout(function() { |
| 88 | + reject(new Error('Hello Promise')); |
| 89 | + }, 0); |
| 90 | + }); |
| 91 | + promise.then(function() { |
| 92 | + fail('should not get here'); |
| 93 | + }); |
| 94 | + setTimeout(function() { |
| 95 | + expectElapsed(log[0].stack, 5); |
| 96 | + done(); |
| 97 | + }, 0); |
86 | 98 | }, 0);
|
87 |
| - }); |
88 |
| - promise.then(function() { |
89 |
| - fail('should not get here'); |
90 |
| - }); |
91 |
| - setTimeout(function() { |
92 |
| - expectElapsed(log[0].stack, 5); |
93 |
| - done(); |
94 | 99 | }, 0);
|
95 |
| - }, 0); |
96 |
| - }, 0); |
97 |
| - }); |
98 |
| - }); |
| 100 | + }); |
| 101 | + }); |
99 | 102 |
|
100 |
| - it('should produce long stack traces when handling error in promise', function(done) { |
101 |
| - lstz.runGuarded(function() { |
102 |
| - setTimeout(function() { |
103 |
| - setTimeout(function() { |
104 |
| - let promise = new Promise(function(resolve, reject) { |
| 103 | + it('should produce long stack traces when handling error in promise', function(done) { |
| 104 | + lstz.runGuarded(function() { |
| 105 | + setTimeout(function() { |
105 | 106 | setTimeout(function() {
|
106 |
| - try { |
107 |
| - throw new Error('Hello Promise'); |
108 |
| - } catch (err) { |
109 |
| - reject(err); |
110 |
| - } |
| 107 | + let promise = new Promise(function(resolve, reject) { |
| 108 | + setTimeout(function() { |
| 109 | + try { |
| 110 | + throw new Error('Hello Promise'); |
| 111 | + } catch (err) { |
| 112 | + reject(err); |
| 113 | + } |
| 114 | + }, 0); |
| 115 | + }); |
| 116 | + promise.catch(function(error) { |
| 117 | + // should be able to get long stack trace |
| 118 | + const longStackFrames: string = longStackTraceZoneSpec.getLongStackTrace(error); |
| 119 | + expectElapsed(longStackFrames, 4); |
| 120 | + done(); |
| 121 | + }); |
111 | 122 | }, 0);
|
112 |
| - }); |
113 |
| - promise.catch(function(error) { |
114 |
| - // should be able to get long stack trace |
115 |
| - const longStackFrames: string = longStackTraceZoneSpec.getLongStackTrace(error); |
116 |
| - expectElapsed(longStackFrames, 4); |
117 |
| - done(); |
118 |
| - }); |
119 |
| - }, 0); |
120 |
| - }, 0); |
121 |
| - }); |
122 |
| - }); |
| 123 | + }, 0); |
| 124 | + }); |
| 125 | + }); |
123 | 126 |
|
124 |
| - it('should not produce long stack traces if Error.stackTraceLimit = 0', function(done) { |
125 |
| - const originalStackTraceLimit = Error.stackTraceLimit; |
126 |
| - lstz.run(function() { |
127 |
| - setTimeout(function() { |
128 |
| - setTimeout(function() { |
| 127 | + it('should not produce long stack traces if Error.stackTraceLimit = 0', function(done) { |
| 128 | + const originalStackTraceLimit = Error.stackTraceLimit; |
| 129 | + lstz.run(function() { |
129 | 130 | setTimeout(function() {
|
130 |
| - if (log[0].stack) { |
131 |
| - expectElapsed(log[0].stack, 1); |
132 |
| - } |
133 |
| - Error.stackTraceLimit = originalStackTraceLimit; |
134 |
| - done(); |
| 131 | + setTimeout(function() { |
| 132 | + setTimeout(function() { |
| 133 | + if (log[0].stack) { |
| 134 | + expectElapsed(log[0].stack, 1); |
| 135 | + } |
| 136 | + Error.stackTraceLimit = originalStackTraceLimit; |
| 137 | + done(); |
| 138 | + }, 0); |
| 139 | + Error.stackTraceLimit = 0; |
| 140 | + throw new Error('Hello'); |
| 141 | + }, 0); |
135 | 142 | }, 0);
|
136 |
| - Error.stackTraceLimit = 0; |
137 |
| - throw new Error('Hello'); |
138 |
| - }, 0); |
139 |
| - }, 0); |
140 |
| - }); |
141 |
| - }); |
142 |
| -}); |
| 143 | + }); |
| 144 | + }); |
| 145 | + })); |
0 commit comments