1
- /* global StackTrace: false */
2
1
describe ( 'StackTrace' , function ( ) {
2
+ var callback ;
3
+ var debugCallback ;
4
+ var errback ;
5
+ var debugErrback ;
6
+
7
+ beforeEach ( function ( ) {
8
+ callback = jasmine . createSpy ( 'callback' ) ;
9
+ errback = jasmine . createSpy ( 'errback' ) ;
10
+ debugCallback = function ( stackframes ) {
11
+ console . log ( stackframes ) ;
12
+ } ;
13
+ debugErrback = function ( e ) {
14
+ console . log ( e . message ) ;
15
+ console . log ( e . stack ) ;
16
+ } ;
17
+ } ) ;
18
+
3
19
describe ( '#constructor' , function ( ) {
4
20
it ( 'should allow empty arguments' , function ( ) {
5
21
expect ( function ( ) {
@@ -9,28 +25,117 @@ describe('StackTrace', function () {
9
25
} ) ;
10
26
11
27
describe ( '#get' , function ( ) {
12
- var unit = new StackTrace ( ) ;
13
28
it ( 'gets stacktrace from current location' , function ( ) {
14
- var stackFrames = unit . get ( ) . filter ( function ( stackFrame ) {
15
- return stackFrame . getFileName ( ) . indexOf ( 'stacktrace-spec.js' ) > - 1 ;
29
+ runs ( function ( ) {
30
+ new StackTrace ( ) . get ( ) . then ( callback , errback ) ;
31
+ } ) ;
32
+ waits ( 100 ) ;
33
+ runs ( function ( ) {
34
+ expect ( callback ) . toHaveBeenCalled ( ) ;
35
+ expect ( callback . mostRecentCall . args [ 0 ] [ 0 ] . fileName ) . toMatch ( / s t a c k t r a c e \- s p e c \. j s \b / ) ;
36
+ expect ( errback ) . not . toHaveBeenCalled ( ) ;
16
37
} ) ;
17
- expect ( stackFrames . length ) . toEqual ( 1 ) ;
18
38
} ) ;
19
39
} ) ;
20
40
21
41
describe ( '#fromError' , function ( ) {
22
- var unit = new StackTrace ( ) ;
42
+ it ( 'rejects with Error given non-Error object' , function ( ) {
43
+ runs ( function ( ) {
44
+ new StackTrace ( ) . fromError ( 'BOGUS' ) . then ( callback , errback ) ;
45
+ } ) ;
46
+ waits ( 100 ) ;
47
+ runs ( function ( ) {
48
+ expect ( callback ) . not . toHaveBeenCalled ( ) ;
49
+ expect ( errback ) . toHaveBeenCalled ( ) ;
50
+ } ) ;
51
+ } ) ;
52
+
23
53
it ( 'parses stacktrace from given Error object' , function ( ) {
24
- var err ;
25
- try {
26
- throw new Error ( 'Yikes!' ) ;
27
- } catch ( e ) {
28
- err = e ;
29
- }
30
- var stackFrames = unit . fromError ( err ) . filter ( function ( stackFrame ) {
31
- return stackFrame . getFileName ( ) . indexOf ( 'stacktrace-spec.js' ) > - 1 ;
54
+ runs ( function ( ) {
55
+ try {
56
+ throw new Error ( 'Yikes!' ) ;
57
+ } catch ( e ) {
58
+ new StackTrace ( ) . fromError ( e ) . then ( callback , errback ) ;
59
+ }
60
+ } ) ;
61
+ waits ( 100 ) ;
62
+ runs ( function ( ) {
63
+ expect ( callback ) . toHaveBeenCalled ( ) ;
64
+ expect ( errback ) . not . toHaveBeenCalled ( ) ;
65
+ } ) ;
66
+ } ) ;
67
+
68
+ it ( 'totally extracts function names' , function ( ) {
69
+ var TEST_FUNCTION = function ( ) {
70
+ try {
71
+ throw new Error ( 'Yikes!' ) ;
72
+ } catch ( e ) {
73
+ function onlySpecSourcesPlease ( stackFrame ) {
74
+ return ( stackFrame . fileName || '' ) . indexOf ( 'stacktrace-spec.js' ) !== - 1 ;
75
+ }
76
+
77
+ new StackTrace ( ) . fromError ( e , { filter : onlySpecSourcesPlease } )
78
+ . then ( callback , errback ) ;
79
+ }
80
+ } ;
81
+ runs ( TEST_FUNCTION ) ;
82
+ waits ( 100 ) ;
83
+ runs ( function ( ) {
84
+ expect ( callback ) . toHaveBeenCalled ( ) ;
85
+ var stackFrames = callback . mostRecentCall . args [ 0 ] ;
86
+ expect ( stackFrames . length ) . toEqual ( 1 ) ;
87
+ expect ( stackFrames [ 0 ] . fileName ) . toMatch ( / s t a c k t r a c e \- s p e c \. j s \b / ) ;
88
+ expect ( stackFrames [ 0 ] . functionName ) . toEqual ( 'TEST_FUNCTION' ) ;
89
+ expect ( errback ) . not . toHaveBeenCalled ( ) ;
90
+ } ) ;
91
+ } ) ;
92
+
93
+ xit ( 'uses source maps to enhance stack frames' , function ( ) {
94
+
95
+ } ) ;
96
+ } ) ;
97
+
98
+ describe ( '#getMappedLocation' , function ( ) {
99
+ var server ;
100
+ beforeEach ( function ( ) {
101
+ server = sinon . fakeServer . create ( ) ;
102
+ } ) ;
103
+ afterEach ( function ( ) {
104
+ server . restore ( ) ;
105
+ } ) ;
106
+
107
+ it ( 'defaults to given stackframe if source map location not found' , function ( ) {
108
+ runs ( function ( ) {
109
+ var stackframe = new StackFrame ( undefined , [ ] , 'http://localhost:9999/test.min.js' , 1 , 32 ) ;
110
+ new StackTrace ( ) . getMappedLocation ( stackframe ) . then ( callback , errback ) ;
111
+ server . requests [ 0 ] . respond ( 404 , { } , '' ) ;
112
+ } ) ;
113
+ waits ( 100 ) ;
114
+ runs ( function ( ) {
115
+ expect ( callback ) . toHaveBeenCalled ( ) ;
116
+ expect ( callback . mostRecentCall . args [ 0 ] ) . toMatchStackFrame ( [ undefined , [ ] , 'http://localhost:9999/test.min.js' , 1 , 32 ] ) ;
117
+ expect ( errback ) . not . toHaveBeenCalled ( ) ;
118
+ } ) ;
119
+ } ) ;
120
+
121
+ it ( 'uses source maps to enhance stack frames' , function ( ) {
122
+ runs ( function ( ) {
123
+ var stackframe = new StackFrame ( undefined , [ ] , 'http://localhost:9999/test.min.js' , 1 , 32 ) ;
124
+ new StackTrace ( ) . getMappedLocation ( stackframe ) . then ( callback , errback ) ;
125
+ var source = 'var foo=function(){};function bar(){}var baz=eval("XXX");\n//@ sourceMappingURL=test.js.map' ;
126
+ server . requests [ 0 ] . respond ( 200 , { 'Content-Type' : 'application/x-javascript' } , source ) ;
127
+ } ) ;
128
+ waits ( 100 ) ;
129
+ runs ( function ( ) {
130
+ var sourceMap = '{"version":3,"sources":["./test.js"],"names":["foo","bar","baz","eval"],"mappings":"AAAA,GAAIA,KAAM,YACV,SAASC,QACT,GAAIC,KAAMC,KAAK","file":"test.min.js"}' ;
131
+ server . requests [ 1 ] . respond ( 200 , { 'Content-Type' : 'application/json' } , sourceMap ) ;
132
+ } ) ;
133
+ waits ( 100 ) ;
134
+ runs ( function ( ) {
135
+ expect ( callback ) . toHaveBeenCalled ( ) ;
136
+ expect ( callback . mostRecentCall . args [ 0 ] ) . toMatchStackFrame ( [ 'bar' , [ ] , './test.js' , 2 , 9 ] ) ;
137
+ expect ( errback ) . not . toHaveBeenCalled ( ) ;
32
138
} ) ;
33
- expect ( stackFrames . length ) . toEqual ( 1 ) ;
34
139
} ) ;
35
140
} ) ;
36
141
0 commit comments