/
test.js
313 lines (277 loc) · 13.9 KB
/
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import os from 'node:os';
import test from 'ava';
import getHomeDirectoryNode from './home-directory.js';
import getHomeDirectoryBrowser from './home-directory-browser.js';
import cleanStack from './index.js';
test('default', t => {
const pre = `Error: foo
at Test.fn (/Users/sindresorhus/dev/clean-stack/test.js:6:15)`;
const stack = `${pre}\n
at MySocket.emit (node:events:365:28)
at MySocket.emit (node:fs/promises:363:28)
at handleMessage (internal/child_process.js:695:10)
at Pipe.channel.onread (internal/child_process.js:440:11)
at process.emit (events.js:172:7)`;
t.is(cleanStack(stack), pre);
});
test('default #2', t => {
const pre = `Error: foo
at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:4:7)`;
const stack = `${pre}\n
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)`;
t.is(cleanStack(stack), pre);
});
test('directly executed node script', t => {
const pre = `Error: foo
at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:4:7)`;
const stack = `${pre}\n
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3`;
t.is(cleanStack(stack), pre);
});
test('internal child_process', t => {
const pre = `Error: foo
at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:4:7)`;
const stack = `${pre}\n
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at internal/child_process.js:696:12`;
t.is(cleanStack(stack), pre);
});
test('internal next_tick', t => {
const pre = `Error: foo
at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:4:7)`;
const stack = `${pre}\n
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)`;
t.is(cleanStack(stack), pre);
});
test('internal various modules', t => {
const pre = `Error: foo
at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:4:7)`;
const stack = `${pre}\n
at emitOne (events.js:101:20)
at process.emit (events.js:188:7)
at process._fatalException (bootstrap_node.js:300:26)`;
t.is(cleanStack(stack), pre);
});
test('babel-polyfill', t => {
const pre = `Error: foo
at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:4:7)`;
const stack = `${pre}\n
at run (/Users/sindresorhus/dev/clean-stack/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:87:22)
at /Users/sindresorhus/dev/clean-stack/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:100:28`;
t.is(cleanStack(stack), pre);
});
test('pirates', t => {
const pre = `Error: foo
at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:4:7)`;
const stack = `${pre}\n
at Module._compile (/Users/zavr/dev/clean-stack/node_modules/pirates/lib/index.js:83:24)
at Object.newLoader [as .js] (/Users/zavr/dev/clean-stack/node_modules/pirates/lib/index.js:88:7)`;
t.is(cleanStack(stack), pre);
});
test('works on Windows', t => {
const expected = `Error: foo
at Test.fn (/Users/sindresorhus/dev/clean-stack/test.js:6:15)`;
const stack = `Error: foo
at Test.fn (\\Users\\sindresorhus\\dev\\clean-stack\\test.js:6:15)
at handleMessage (internal\\child_process.js:695:10)
at Pipe.channel.onread (internal\\child_process.js:440:11)
at process.emit (events.js:172:7)`;
t.is(cleanStack(stack), expected);
});
test('works with Electron stack traces - dev app', t => {
const expected = `Error: foo
at Object.<anonymous> (/Users/sindresorhus/dev/electron-unhandled/fixture-rejection.js:17:16)
at Object.<anonymous> (/Users/sindresorhus/dev/electron-unhandled/fixture-rejection.js:19:3)`;
const stack = `Error: foo
at Object.<anonymous> (/Users/sindresorhus/dev/electron-unhandled/fixture-rejection.js:17:16)
at Object.<anonymous> (/Users/sindresorhus/dev/electron-unhandled/fixture-rejection.js:19:3)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at loadApplicationPackage (/Users/sindresorhus/dev/electron-unhandled/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:283:12)
at Object.<anonymous> (/Users/sindresorhus/dev/electron-unhandled/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:325:5)
at Object.<anonymous> (/Users/sindresorhus/dev/electron-unhandled/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:361:3)`;
t.is(cleanStack(stack), expected);
});
test('works with Electron stack traces - built app', t => {
const expected = `Error: foo
at Object.<anonymous> (/Users/sindresorhus/dev/forks/kap/dist/mac/Kap.app/Contents/Resources/app/dist/main/index.js:107:16)
at Object.<anonymous> (/Users/sindresorhus/dev/forks/kap/dist/mac/Kap.app/Contents/Resources/app/dist/main/index.js:568:3)`;
const stack = `Error: foo
at Object.<anonymous> (/Users/sindresorhus/dev/forks/kap/dist/mac/Kap.app/Contents/Resources/app/dist/main/index.js:107:16)
at Object.<anonymous> (/Users/sindresorhus/dev/forks/kap/dist/mac/Kap.app/Contents/Resources/app/dist/main/index.js:568:3)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Object.<anonymous> (/Users/sindresorhus/dev/forks/kap/dist/mac/Kap.app/Contents/Resources/electron.asar/browser/init.js:171:8)
at Object.<anonymous> (/Users/sindresorhus/dev/forks/kap/dist/mac/Kap.app/Contents/Resources/electron.asar/browser/init.js:173:3)
at Module._compile (module.js:571:32)`;
t.is(cleanStack(stack), expected);
});
test('`pretty` option', t => {
const stack = `Error: foo
at Test.fn (${os.homedir()}/dev/clean-stack/test.js:6:15)
at handleMessage (internal/child_process.js:695:10)
at Pipe.channel.onread (internal/child_process.js:440:11)
at process.emit (events.js:172:7)`;
const expected = `Error: foo
at Test.fn (~/dev/clean-stack/test.js:6:15)`;
t.is(cleanStack(stack, {pretty: true}), expected);
});
test('`basePath` option', t => {
const basePath = '/Users/foo/dev/';
const stack = `Error: with basePath
at Object.<anonymous> (/Users/foo/dev/node_modules/foo/bar.js:1:14)
at /Users/foo/dev/node_modules/foo/baz.js:1:14
at Module._compile (internal/modules/cjs/loader.js:1200:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47`;
const expected = `Error: with basePath
at Object.<anonymous> (node_modules/foo/bar.js:1:14)
at node_modules/foo/baz.js:1:14`;
t.is(cleanStack(stack, {basePath}), expected);
});
test('`basePath` option should have precedence over `pretty` option', t => {
const basePath = `${os.homedir()}/dev/`;
const stack = `Error: with basePath
at Object.<anonymous> (${os.homedir()}/dev/node_modules/foo/bar.js:1:14)
at Module._compile (internal/modules/cjs/loader.js:1200:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47`;
const expected = `Error: with basePath
at Object.<anonymous> (node_modules/foo/bar.js:1:14)`;
t.is(cleanStack(stack, {basePath, pretty: true}), expected);
});
test('`basePath` option should ignore trailing slash', t => {
const basePath = `${os.homedir()}/dev`;
const stack = `Error: with basePath
at Object.<anonymous> (${os.homedir()}/dev/node_modules/foo/bar.js:1:14)
at Module._compile (internal/modules/cjs/loader.js:1200:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47`;
const expected = `Error: with basePath
at Object.<anonymous> (node_modules/foo/bar.js:1:14)`;
t.is(cleanStack(stack, {basePath, pretty: true}), expected);
});
test('`basePath` option should support file URLs', t => {
const basePath = '/dev';
const stack = `Error
at file:///dev/node_modules/foo/bar.js:1:14
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:527:24)
at async loadESM (node:internal/process/esm_loader:91:5)
at async handleMainPromise (node:internal/modules/run_main:65:12)`;
const expected = `Error
at node_modules/foo/bar.js:1:14
at async Promise.all (index 0)`;
t.is(cleanStack(stack, {basePath, pretty: true}), expected);
});
test('`pathFilter` option allows for excluding custom lines if the callback returns true', t => {
const pathFilter = path => !/home-directory/.test(path);
const pre = `Error: foo
at Test.fn (/Users/sindresorhus/dev/clean-stack/test.js:6:15)`;
const stack = `${pre}\n
at getHomeDirectory (/Users/sindresorhus/dev/clean-stack/home-directory.js:3:32)
at MySocket.emit (node:events:365:28)
at MySocket.emit (node:fs/promises:363:28)
at handleMessage (internal/child_process.js:695:10)
at Pipe.channel.onread (internal/child_process.js:440:11)
at process.emit (events.js:172:7)`;
t.is(cleanStack(stack, {pathFilter}), pre);
});
test('`pathFilter` option keeps custom lines if the callback returns false', t => {
const pathFilterCallback = () => true;
const pre = `Error: foo
at Test.fn (/Users/sindresorhus/dev/clean-stack/test.js:6:15)
at getHomeDirectory (/Users/sindresorhus/dev/clean-stack/home-directory.js:3:32)`;
const stack = `${pre}\n
at MySocket.emit (node:events:365:28)
at MySocket.emit (node:fs/promises:363:28)
at handleMessage (internal/child_process.js:695:10)
at Pipe.channel.onread (internal/child_process.js:440:11)
at process.emit (events.js:172:7)`;
t.is(cleanStack(stack, {pathFilterCallback}), pre);
});
test('new stack format on Node.js 15 and later', t => {
const stack = `Error
at B (/home/fengkx/projects/test/stack.js:5:19)
at A (/home/fengkx/projects/test/stack.js:7:9)
at Object.<anonymous> (/home/fengkx/projects/test/stack.js:14:1)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10)
at Module.load (node:internal/modules/cjs/loader:967:32)
at Function.Module._load (node:internal/modules/cjs/loader:807:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47`;
const expected = `Error
at B (/home/fengkx/projects/test/stack.js:5:19)
at A (/home/fengkx/projects/test/stack.js:7:9)
at Object.<anonymous> (/home/fengkx/projects/test/stack.js:14:1)`;
t.is(cleanStack(stack), expected);
});
test('at async', t => {
const basePath = '/base';
const stack = `Error: test
at /base/test.js:704:48
at /base/node_modules/vitest/dist/chunk-runtime-chain.072b5677.js:2266:13
at /base/node_modules/vitest/dist/chunk-runtime-chain.072b5677.js:2141:26
at runTest (/base/node_modules/vitest/dist/chunk-runtime-error.b043a88d.js:550:42)
at async runSuite (/base/node_modules/vitest/dist/chunk-runtime-error.b043a88d.js:645:15)
at async runFiles (/base/node_modules/vitest/dist/chunk-runtime-error.b043a88d.js:779:5)
at async startTestsNode (/base/node_modules/vitest/dist/chunk-runtime-error.b043a88d.js:797:3)
at async /base/node_modules/vitest/dist/entry.js:94:11
at async Module.withEnv (/base/node_modules/vitest/dist/chunk-runtime-error.b043a88d.js:295:5)
at async run (/base/node_modules/vitest/dist/entry.js:87:7)
at async file:///base/node_modules/tinypool/dist/esm/worker.js:109:20
at file:///base/node_modules/tinypool/dist/esm/worker.js:109:20`;
const expected = `Error: test
at test.js:704:48
at node_modules/vitest/dist/chunk-runtime-chain.072b5677.js:2266:13
at node_modules/vitest/dist/chunk-runtime-chain.072b5677.js:2141:26
at runTest (node_modules/vitest/dist/chunk-runtime-error.b043a88d.js:550:42)
at async runSuite (node_modules/vitest/dist/chunk-runtime-error.b043a88d.js:645:15)
at async runFiles (node_modules/vitest/dist/chunk-runtime-error.b043a88d.js:779:5)
at async startTestsNode (node_modules/vitest/dist/chunk-runtime-error.b043a88d.js:797:3)
at async node_modules/vitest/dist/entry.js:94:11
at async Module.withEnv (node_modules/vitest/dist/chunk-runtime-error.b043a88d.js:295:5)
at async run (node_modules/vitest/dist/entry.js:87:7)
at async node_modules/tinypool/dist/esm/worker.js:109:20
at node_modules/tinypool/dist/esm/worker.js:109:20`;
t.is(cleanStack(stack, {basePath}), expected);
});
test('handle undefined', t => {
const stack = undefined;
const expected = undefined;
t.is(cleanStack(stack, {pretty: true}), expected);
});
test('exports for home-directory files match', t => {
t.is(typeof getHomeDirectoryNode, 'function');
t.is(typeof getHomeDirectoryBrowser, 'function');
});