-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
resolve-error-location.test.js
138 lines (126 loc) · 6.11 KB
/
resolve-error-location.test.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict';
const expect = require('chai').expect;
const resolveErrorLocation = require('../../../../../lib/utils/telemetry/resolve-error-location');
const tokenizeException = require('../../../../../lib/utils/tokenize-exception');
describe('test/unit/lib/utils/resolve-error-location.test.js', () => {
it('should be null when stack missing', () => {
const err = new Error('test');
delete err.stack;
const result = resolveErrorLocation(tokenizeException(err));
expect(result).to.equal('<not accessible due to non-error exception>');
});
it('should be null for error with code and one-line stacktrace', () => {
const err = new Error('test');
err.code = 'ERR_CODE';
err.stack = 'Oneline stacktrace';
const result = resolveErrorLocation(tokenizeException(err));
expect(result).to.equal('<not available>');
});
it('should be null if no matching lines found', () => {
const err = new Error('test');
err.stack = 'no matching\nlines in\nstacktrace';
const result = resolveErrorLocation(tokenizeException(err));
expect(result).to.equal('<not reflected in stack>');
});
it('should not capture non-stacktrace paths', () => {
const err = new Error('test');
err.stack =
'Could not find a version that satisfies the requirement flask (from versions: none) No matching distribution found for flask.';
const result = resolveErrorLocation(tokenizeException(err));
expect(result).to.equal('<not reflected in stack>');
});
if (process.platform !== 'win32') {
it('should correctly handle paths not enclosed in parentheses', () => {
const err = new Error('test');
err.stack =
'Error: spawn E2BIG\n' +
' at ChildProcess.spawn (node:internal/child_process:403:11)\n' +
' at Object.spawn (node:child_process:573:9)\n' +
' at /home/xxx/api/node_modules/bestzip/lib/bestzip.js:75:29\n' +
' at /home/xxx/api/node_modules/async/dist/async.js:1802:20\n';
const result = resolveErrorLocation(tokenizeException(err));
expect(result).to.equal(
[
'node:internal/child_process:403:11',
'node:child_process:573:9',
'/bestzip/lib/bestzip.js:75:29',
'/async/dist/async.js:1802:20',
].join('\n')
);
});
it('should return at most 7 lines', () => {
const err = new Error('test');
err.stack =
'Error:\n' +
' at Context.it (/home/xxx/serverless/test/unit/lib/utils/resolve-error-location.test.js:10:17)\n' +
' at callFn (/home/xxx/serverless/node_modules/mocha/lib/runnable.js:366:21)\n' +
' at Test.Runnable.run (/home/xxx/serverless/node_modules/mocha/lib/runnable.js:354:5)\n' +
' at Runner.runTest (/home/xxx/serverless/node_modules/mocha/lib/runner.js:677:10)\n' +
' at next (/home/xxx/serverless/node_modules/mocha/lib/runner.js:801:12)\n' +
' at next (/home/xxx/serverless/node_modules/mocha/lib/runner.js:802:12)\n' +
' at next (/home/xxx/serverless/node_modules/mocha/lib/runner.js:803:12)\n' +
' at next (/home/xxx/serverless/node_modules/mocha/lib/runner.js:804:12)\n' +
' at next (/home/xxx/serverless/node_modules/mocha/lib/runner.js:805:12)\n' +
' at next (/home/xxx/serverless/node_modules/mocha/lib/runner.js:594:14)\n';
const result = resolveErrorLocation(tokenizeException(err));
expect(result).to.equal(
[
'/test/unit/lib/utils/resolve-error-location.test.js:10:17',
'/node_modules/mocha/lib/runnable.js:366:21',
'^:354:5',
'/node_modules/mocha/lib/runner.js:677:10',
'^:801:12',
'^:802:12',
'^:803:12',
].join('\n')
);
});
}
if (process.platform === 'win32') {
it('should correctly handle paths not enclosed in parentheses', () => {
const err = new Error('test');
err.stack =
'Error: spawn E2BIG\r\n' +
' at ChildProcess.spawn (node:internal/child_process:403:11)\r\n' +
' at Object.spawn (node:child_process:573:9)\r\n' +
' at C:\\home\\xxx\\api\\node_modules\\bestzip\\lib\\bestzip.js:75:29\r\n' +
' at C:\\home\\xxx\\api\\node_modules\\async\\dist\\async.js:1802:20\r\n';
const result = resolveErrorLocation(tokenizeException(err));
expect(result).to.equal(
[
'node:internal/child_process:403:11',
'node:child_process:573:9',
'/bestzip/lib/bestzip.js:75:29',
'/async/dist/async.js:1802:20',
].join('\n')
);
});
it('should return at most 7 lines and use `/` path separator', () => {
const err = new Error('test');
err.stack =
'Error:\n' +
' at Context.it (C:\\home\\xxx\\serverless\\test\\unit\\lib\\utils\\resolve-error-location.test.js:10:17)\r\n' +
' at callFn (C:\\home\\xxx\\serverless\\node_modules\\mocha\\lib\\runnable.js:366:21)\r\n' +
' at Test.Runnable.run (C:\\home\\xxx\\serverless\\node_modules\\mocha\\lib\\runnable.js:354:5)\r\n' +
' at Runner.runTest (C:\\home\\xxx\\serverless\\node_modules\\mocha\\lib\\runner.js:677:10)\r\n' +
' at next (C:\\home\\xxx\\serverless\\node_modules\\mocha\\lib\\runner.js:801:12)\r\n' +
' at next (C:\\home\\xxx\\serverless\\node_modules\\mocha\\lib\\runner.js:802:12)\r\n' +
' at next (C:\\home\\xxx\\serverless\\node_modules\\mocha\\lib\\runner.js:803:12)\r\n' +
' at next (C:\\home\\xxx\\serverless\\node_modules\\mocha\\lib\\runner.js:804:12)\r\n' +
' at next (C:\\home\\xxx\\serverless\\node_modules\\mocha\\lib\\runner.js:805:12)\r\n' +
' at next (C:\\home\\xxx\\serverless\\node_modules\\mocha\\lib\\runner.js:594:14)\r\n';
const result = resolveErrorLocation(tokenizeException(err));
expect(result).to.equal(
[
'/test/unit/lib/utils/resolve-error-location.test.js:10:17',
'/node_modules/mocha/lib/runnable.js:366:21',
'^:354:5',
'/node_modules/mocha/lib/runner.js:677:10',
'^:801:12',
'^:802:12',
'^:803:12',
].join('\n')
);
});
}
});