@@ -8,15 +8,15 @@ import { getError } from './helper/error';
8
8
9
9
describe ( 'parsers.ts' , ( ) => {
10
10
let frames : stacktrace . StackFrame [ ] ;
11
- let spy : jest . SpyInstance ;
11
+ let readFileSpy : jest . SpyInstance ;
12
12
let contextLines : ContextLines ;
13
13
14
- async function addContext ( frames : StackFrame [ ] ) : Promise < void > {
15
- await contextLines . addToEvent ( { exception : { values : [ { stacktrace : { frames } } ] } } ) ;
14
+ async function addContext ( frames : StackFrame [ ] , lines : number = 7 ) : Promise < void > {
15
+ await contextLines . addSourceContext ( { exception : { values : [ { stacktrace : { frames } } ] } } , lines ) ;
16
16
}
17
17
18
18
beforeEach ( ( ) => {
19
- spy = jest . spyOn ( fs , 'readFile' ) ;
19
+ readFileSpy = jest . spyOn ( fs , 'readFile' ) ;
20
20
frames = stacktrace . parse ( new Error ( 'test' ) ) ;
21
21
contextLines = new ContextLines ( ) ;
22
22
resetFileContentCache ( ) ;
@@ -30,16 +30,16 @@ describe('parsers.ts', () => {
30
30
test ( 'parseStack with same file' , async ( ) => {
31
31
expect . assertions ( 1 ) ;
32
32
33
- let mockCalls = 0 ;
34
33
let parsedFrames = Parsers . parseStack ( frames ) ;
35
34
await addContext ( parsedFrames ) ;
36
35
37
- mockCalls = spy . mock . calls . length ;
36
+ const numCalls = readFileSpy . mock . calls . length ;
38
37
parsedFrames = Parsers . parseStack ( frames ) ;
39
38
await addContext ( parsedFrames ) ;
40
39
41
- // Calls to readFile shouldn't increase if there isn't a new error
42
- expect ( spy ) . toHaveBeenCalledTimes ( mockCalls ) ;
40
+ // Calls to `readFile` shouldn't increase if there isn't a new error to
41
+ // parse whose stacktrace contains a file we haven't yet seen
42
+ expect ( readFileSpy ) . toHaveBeenCalledTimes ( numCalls ) ;
43
43
} ) ;
44
44
45
45
test ( 'parseStack with ESM module names' , async ( ) => {
@@ -58,27 +58,20 @@ describe('parsers.ts', () => {
58
58
] ;
59
59
const parsedFrames = Parsers . parseStack ( framesWithFilePath ) ;
60
60
await addContext ( parsedFrames ) ;
61
- expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
61
+ expect ( readFileSpy ) . toHaveBeenCalledTimes ( 1 ) ;
62
62
} ) ;
63
63
64
64
test ( 'parseStack with adding different file' , async ( ) => {
65
- expect . assertions ( 2 ) ;
66
- let mockCalls = 0 ;
67
- let newErrorCalls = 0 ;
65
+ expect . assertions ( 1 ) ;
68
66
let parsedFrames = Parsers . parseStack ( frames ) ;
69
67
await addContext ( parsedFrames ) ;
70
68
71
- mockCalls = spy . mock . calls . length ;
69
+ const numCalls = readFileSpy . mock . calls . length ;
72
70
parsedFrames = Parsers . parseStack ( stacktrace . parse ( getError ( ) ) ) ;
73
71
await addContext ( parsedFrames ) ;
74
72
75
- newErrorCalls = spy . mock . calls . length ;
76
- expect ( newErrorCalls ) . toBeGreaterThan ( mockCalls ) ;
77
-
78
- parsedFrames = Parsers . parseStack ( stacktrace . parse ( getError ( ) ) ) ;
79
- await addContext ( parsedFrames ) ;
80
-
81
- expect ( spy ) . toHaveBeenCalledTimes ( newErrorCalls ) ;
73
+ const newErrorCalls = readFileSpy . mock . calls . length ;
74
+ expect ( newErrorCalls ) . toBeGreaterThan ( numCalls ) ;
82
75
} ) ;
83
76
84
77
test ( 'parseStack with duplicate files' , async ( ) => {
@@ -115,16 +108,14 @@ describe('parsers.ts', () => {
115
108
116
109
const parsedFrames = Parsers . parseStack ( framesWithDuplicateFiles ) ;
117
110
await addContext ( parsedFrames ) ;
118
- expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
111
+ expect ( readFileSpy ) . toHaveBeenCalledTimes ( 1 ) ;
119
112
} ) ;
120
113
121
114
test ( 'parseStack with no context' , async ( ) => {
122
- contextLines = new ContextLines ( { frameContextLines : 0 } ) ;
123
-
124
115
expect . assertions ( 1 ) ;
125
116
const parsedFrames = Parsers . parseStack ( frames ) ;
126
- await addContext ( parsedFrames ) ;
127
- expect ( spy ) . toHaveBeenCalledTimes ( 0 ) ;
117
+ await addContext ( parsedFrames , 0 ) ;
118
+ expect ( readFileSpy ) . toHaveBeenCalledTimes ( 0 ) ;
128
119
} ) ;
129
120
} ) ;
130
121
} ) ;
0 commit comments