-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcontextlines.test.ts
115 lines (100 loc) · 3.35 KB
/
contextlines.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { describe, expect, it } from 'vitest';
import type { StackFrame } from '@sentry/core';
import { applySourceContextToFrame } from '../../src/integrations/contextlines';
const lines = ['line1', 'line2', 'line3', 'line4', 'line5', 'line6', 'line7', 'line8', 'line9'];
describe('ContextLines', () => {
describe('applySourceContextToFrame', () => {
it.each([
[
5,
{
pre_context: ['line2', 'line3', 'line4'],
context_line: 'line5',
post_context: ['line6', 'line7', 'line8'],
},
],
[
1,
{
pre_context: [],
context_line: 'line1',
post_context: ['line2', 'line3', 'line4'],
},
],
[
2,
{
pre_context: ['line1'],
context_line: 'line2',
post_context: ['line3', 'line4', 'line5'],
},
],
[
9,
{
pre_context: ['line6', 'line7', 'line8'],
context_line: 'line9',
post_context: [],
},
],
])(
'correctly applies pre, post contexts and context lines for an inline stack frame (lineno %s)',
(lineno, contextLines) => {
const frame: StackFrame = {
lineno,
filename: 'https://mydomain.com/index.html',
};
expect(applySourceContextToFrame(frame, lines, 'https://mydomain.com/index.html', 3)).toStrictEqual({
filename: 'https://mydomain.com/index.html',
lineno,
...contextLines,
});
},
);
it('only applies the context line if the range is 0', () => {
const frame: StackFrame = {
lineno: 5,
filename: 'https://mydomain.com/index.html',
};
expect(applySourceContextToFrame(frame, lines, 'https://mydomain.com/index.html', 0)).toStrictEqual({
filename: 'https://mydomain.com/index.html',
lineno: 5,
context_line: 'line5',
pre_context: [],
post_context: [],
});
});
it("no-ops if the frame's line number is out of bounds for the found lines", () => {
const frame: StackFrame = {
lineno: 20,
filename: 'https://mydomain.com/index.html',
};
expect(applySourceContextToFrame(frame, lines, 'https://mydomain.com/index.html', 3)).toStrictEqual(frame);
});
it("no-ops if the frame's filename is not the html file's name", () => {
const frame: StackFrame = {
filename: '/someScript.js',
};
expect(applySourceContextToFrame(frame, lines, 'https://mydomain.com/index.html', 3)).toStrictEqual(frame);
});
it("no-ops if the frame doesn't have a line number", () => {
const frame: StackFrame = {
filename: '/index.html',
};
expect(applySourceContextToFrame(frame, lines, 'https://mydomain.com/index.html', 0)).toStrictEqual(frame);
});
it("no-ops if the frame doesn't have a filename", () => {
const frame: StackFrame = {
lineno: 9,
};
expect(applySourceContextToFrame(frame, lines, 'https://mydomain.com/index.html', 0)).toStrictEqual(frame);
});
it('no-ops if there are no html lines available', () => {
const frame: StackFrame = {
lineno: 9,
filename: '/index.html',
};
expect(applySourceContextToFrame(frame, [], 'https://mydomain.com/index.html', 0)).toStrictEqual(frame);
});
});
});