-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathcapture-stack-trace.test.js
More file actions
841 lines (708 loc) · 22.3 KB
/
capture-stack-trace.test.js
File metadata and controls
841 lines (708 loc) · 22.3 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
import { nativeFrameForTesting } from "bun:internal-for-testing";
import { noInline } from "bun:jsc";
import { afterEach, expect, mock, test } from "bun:test";
const origPrepareStackTrace = Error.prepareStackTrace;
afterEach(() => {
Error.prepareStackTrace = origPrepareStackTrace;
});
test("Regular .stack", () => {
var err;
class Foo {
constructor() {
err = new Error("wat");
}
}
new Foo();
expect(err.stack).toMatch(/at new Foo/);
});
test("throw inside Error.prepareStackTrace doesnt crash", () => {
Error.prepareStackTrace = function (err, stack) {
Error.prepareStackTrace = null;
throw new Error("wat");
};
expect(() => new Error().stack).toThrow("wat");
});
test("capture stack trace", () => {
function f1() {
f2();
}
function f2() {
f3();
}
function f3() {
logErrorStackTrace();
}
function logErrorStackTrace() {
let error = {};
Error.captureStackTrace(error);
expect(error.stack !== undefined).toBe(true);
}
f1();
});
test("capture stack trace with message", () => {
function f1() {
f2();
}
function f2() {
f3();
}
function f3() {
logErrorStackTrace();
}
function logErrorStackTrace() {
let e1 = { message: "bad error!" };
Error.captureStackTrace(e1);
expect(e1.message === "bad error!").toBe(true);
let e2 = new Error("bad error!");
Error.captureStackTrace(e2);
expect(e2.message === "bad error!").toBe(true);
}
f1();
});
test("capture stack trace with constructor", () => {
class S {
constructor() {
captureStackTrace();
}
}
function captureStackTrace() {
let e1 = {};
Error.captureStackTrace(e1);
expect(e1.stack.split("\n")[2].includes("new S")).toBe(true);
}
let s = new S();
});
test("capture stack trace limit", () => {
function f1() {
f2();
}
function f2() {
f3();
}
function f3() {
f4();
}
function f4() {
f5();
}
function f5() {
f6();
}
function f6() {
f7();
}
function f7() {
f8();
}
function f8() {
f9();
}
function f9() {
f10();
}
function f10() {
captureStackTrace();
}
var originalLimit = Error.stackTraceLimit;
function captureStackTrace() {
let e1 = {};
Error.captureStackTrace(e1);
expect(e1.stack.split("\n").length).toBe(11);
let e2 = new Error();
Error.captureStackTrace(e2);
expect(e2.stack.split("\n").length).toBe(11);
let e3 = {};
Error.stackTraceLimit = 4;
Error.captureStackTrace(e3);
expect(e3.stack.split("\n").length).toBe(5);
let e4 = new Error();
Error.captureStackTrace(e4);
expect(e4.stack.split("\n").length).toBe(5);
let e5 = { stackTraceLimit: 2 };
Error.captureStackTrace(e5);
expect(e5.stack.split("\n").length).toBe(5);
let e6 = {};
Error.stackTraceLimit = Infinity;
Error.captureStackTrace(e6);
expect(e6.stack.split("\n").length).toBe(13);
}
try {
f1();
} finally {
Error.stackTraceLimit = originalLimit;
}
});
test("prepare stack trace", () => {
function f1() {
f2();
}
function f2() {
let e = {};
let prevPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (e, stack) => {
return "custom stack trace";
};
Error.captureStackTrace(e);
expect(e.stack).toBe("custom stack trace");
Error.prepareStackTrace = prevPrepareStackTrace;
f3();
}
function f3() {
let e = { message: "bad error!" };
let prevPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (e, s) => {
expect(e.message === "bad error!").toBe(true);
expect(s.length).toBe(4);
};
Error.stackTraceLimit = 10;
Error.captureStackTrace(e);
expect(e.stack === undefined).toBe(true);
Error.prepareStackTrace = prevPrepareStackTrace;
}
f1();
});
test("capture stack trace second argument", () => {
function f0() {
let s = new S();
}
class S {
constructor() {
f1();
}
}
function f1() {
f2();
}
function f2() {
f3();
}
function f3() {
f4();
}
function f4() {
f5();
}
function f5() {
f6();
}
function f6() {
let e = { message: "bad error!" };
Error.captureStackTrace(e);
expect(e.stack.split("\n")[1].includes("at f6")).toBe(true);
expect(e.stack.split("\n")[2].includes("at f5")).toBe(true);
let e2 = {};
Error.captureStackTrace(e2, f3);
expect(e2.stack.split("\n")[1].includes("at f2")).toBe(true);
expect(e2.stack.split("\n")[2].includes("at f1")).toBe(true);
let e3 = {};
Error.captureStackTrace(e3, f9);
expect(e3.stack.split("\n").length).toBe(1);
let e4 = { message: "exclude constructor!" };
Error.captureStackTrace(e4, S.constructor);
expect(e4.stack.split("\n").length).toBe(1);
let e5 = { message: "actually exclude constructor!" };
Error.captureStackTrace(e5, S);
expect(e5.stack.split("\n")[1].includes("at f0")).toBe(true);
}
function f9() {
// nothing
}
f0();
});
test("capture stack trace edge cases", () => {
let e1 = {};
Error.captureStackTrace(e1, null);
expect(e1.stack !== undefined).toBe(true);
let e2 = {};
Error.captureStackTrace(e2, undefined);
expect(e2.stack !== undefined).toBe(true);
let e3 = {};
Error.captureStackTrace(e3, 1);
expect(e3.stack !== undefined).toBe(true);
let e4 = {};
Error.captureStackTrace(e4, "foo");
expect(e4.stack !== undefined).toBe(true);
let e5 = {};
Error.captureStackTrace(e5, {});
expect(e5.stack !== undefined).toBe(true);
expect(Error.captureStackTrace({})).toBe(undefined);
expect(Error.captureStackTrace({}, () => {})).toBe(undefined);
expect(Error.captureStackTrace({}, undefined)).toBe(undefined);
expect(Error.captureStackTrace({}, null)).toBe(undefined);
expect(Error.captureStackTrace({}, 1)).toBe(undefined);
expect(Error.captureStackTrace({}, "foo")).toBe(undefined);
expect(Error.captureStackTrace({}, {})).toBe(undefined);
expect(Error.captureStackTrace({}, [])).toBe(undefined);
expect(Error.captureStackTrace({}, true)).toBe(undefined);
});
test("prepare stack trace call sites", () => {
function f1() {
f2();
}
function f2() {
f3();
}
function f3() {
let e = { message: "bad error!" };
// let e = new Error("bad error!");
let prevPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (e, s) => {
expect(s[0].getThis !== undefined).toBe(true);
expect(s[0].getTypeName !== undefined).toBe(true);
expect(s[0].getFunction !== undefined).toBe(true);
expect(s[0].getFunctionName !== undefined).toBe(true);
expect(s[0].getMethodName !== undefined).toBe(true);
expect(s[0].getFileName !== undefined).toBe(true);
expect(s[0].getLineNumber !== undefined).toBe(true);
expect(s[0].getColumnNumber !== undefined).toBe(true);
expect(s[0].getEvalOrigin !== undefined).toBe(true);
expect(s[0].isToplevel !== undefined).toBe(true);
expect(s[0].isEval !== undefined).toBe(true);
expect(s[0].isNative !== undefined).toBe(true);
expect(s[0].isConstructor !== undefined).toBe(true);
expect(s[0].isAsync !== undefined).toBe(true);
expect(s[0].isPromiseAll !== undefined).toBe(true);
expect(s[0].getPromiseIndex !== undefined).toBe(true);
};
Error.captureStackTrace(e);
expect(e.stack === undefined).toBe(true);
Error.prepareStackTrace = prevPrepareStackTrace;
}
f1();
});
test("sanity check", () => {
function f1() {
f2();
}
function f2() {
f3();
}
function f3() {
let e = new Error("bad error!");
let prevPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (e, s) => {
// getThis returns undefined in strict mode
expect(s[0].getThis()).toBe(undefined);
expect(s[0].getTypeName()).toBe("undefined");
// getFunction returns undefined in strict mode
expect(s[0].getFunction()).toBe(undefined);
expect(s[0].getFunctionName()).toBe("f3");
expect(s[0].getMethodName()).toBe("f3");
expect(typeof s[0].getLineNumber()).toBe("number");
expect(typeof s[0].getColumnNumber()).toBe("number");
expect(s[0].getFileName().includes("capture-stack-trace.test.js")).toBe(true);
expect(s[0].getEvalOrigin()).toBe(undefined);
expect(s[0].isToplevel()).toBe(true);
expect(s[0].isEval()).toBe(false);
expect(s[0].isNative()).toBe(false);
expect(s[0].isConstructor()).toBe(false);
expect(s[0].isAsync()).toBe(false);
expect(s[0].isPromiseAll()).toBe(false);
expect(s[0].getPromiseIndex()).toBe(null);
};
Error.captureStackTrace(e);
expect(e.stack === undefined).toBe(true);
Error.prepareStackTrace = prevPrepareStackTrace;
}
f1();
});
test("CallFrame isEval works as expected", () => {
let prevPrepareStackTrace = Error.prepareStackTrace;
let name, fn;
Error.prepareStackTrace = (e, s) => {
return s;
};
name = "f1";
const stack = eval(`(function ${name}() {
return new Error().stack;
})()`);
Error.prepareStackTrace = prevPrepareStackTrace;
// TODO: 0 and 1 should both return true here.
expect(stack[1].isEval()).toBe(true);
expect(stack[0].getFunctionName()).toBe(name);
});
test("CallFrame isTopLevel returns false for Function constructor", () => {
let prevPrepareStackTrace = Error.prepareStackTrace;
const sloppyFn = new Function("let e=new Error();Error.captureStackTrace(e);return e.stack");
sloppyFn.displayName = "sloppyFnWow";
noInline(sloppyFn);
const that = {};
Error.prepareStackTrace = (e, s) => {
expect(s[0].getFunctionName()).toBe(sloppyFn.displayName);
expect(s[0].getFunction()).toBe(sloppyFn);
expect(s[0].isToplevel()).toBe(false);
expect(s[0].isEval()).toBe(false);
// Strict-mode functions shouldn't have getThis or getFunction
// available.
expect(s[1].getThis()).toBe(undefined);
expect(s[1].getFunction()).toBe(undefined);
};
sloppyFn.call(that);
Error.prepareStackTrace = prevPrepareStackTrace;
});
test("CallFrame.p.getThisgetFunction: strict/sloppy mode interaction", () => {
let prevPrepareStackTrace = Error.prepareStackTrace;
const strictFn = new Function('"use strict";let e=new Error();Error.captureStackTrace(e);return e.stack');
const sloppyFn = new Function("x", "x()");
const that = {};
Error.prepareStackTrace = (e, s) => {
// The first strict mode function encounted during stack unwinding
// stops subsequent frames from having getThis\getFunction.
for (const t of s) {
expect(t.getThis()).toBe(undefined);
expect(t.getFunction()).toBe(undefined);
}
};
sloppyFn.call(that, strictFn);
Error.prepareStackTrace = prevPrepareStackTrace;
});
test("CallFrame.p.isConstructor", () => {
let prevPrepareStackTrace = Error.prepareStackTrace;
class C {
constructor() {
Error.captureStackTrace(new Error(""));
}
}
Error.prepareStackTrace = (e, s) => {
expect(s[0].isConstructor()).toBe(true);
// TODO: should be false: this is an instance of C
expect(s[0].isToplevel()).toBe(true);
// TODO: should return the class name
// expect(s[0].getTypeName()).toBe('C');
expect(s[1].isConstructor()).toBe(false);
expect(s[1].isToplevel()).toBe(true);
};
new C();
Error.prepareStackTrace = prevPrepareStackTrace;
});
test("CallFrame.p.isNative", () => {
let prevPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (e, s) => {
expect(s[0].isNative()).toBe(false);
expect(s[1].isNative()).toBe(true);
expect(s[2].isNative()).toBe(false);
};
nativeFrameForTesting(() => {
const err = new Error("");
Error.captureStackTrace(err);
return 0;
});
Error.prepareStackTrace = prevPrepareStackTrace;
});
test("return non-strings from Error.prepareStackTrace", () => {
// This behavior is allowed by V8 and used by the node-depd npm package.
let prevPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (e, s) => s;
const e = new Error();
Error.captureStackTrace(e);
expect(Array.isArray(e.stack)).toBe(true);
Error.prepareStackTrace = prevPrepareStackTrace;
});
test("CallFrame.p.toString", () => {
let prevPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (e, s) => s;
const e = new Error();
Error.captureStackTrace(e);
expect(e.stack[0].toString().includes("<anonymous>")).toBe(true);
});
// TODO: line numbers are wrong in a release build
test("err.stack should invoke prepareStackTrace", () => {
var lineNumber = -1;
var functionName = "";
var parentLineNumber = -1;
function functionWithAName() {
// This is V8's behavior.
let prevPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (e, s) => {
lineNumber = s[0].getLineNumber();
functionName = s[0].getFunctionName();
parentLineNumber = s[1].getLineNumber();
expect(s[0].getFileName().includes("capture-stack-trace.test.js")).toBe(true);
expect(s[1].getFileName().includes("capture-stack-trace.test.js")).toBe(true);
};
const e = new Error();
e.stack;
Error.prepareStackTrace = prevPrepareStackTrace;
}
functionWithAName();
expect(functionName).toBe("functionWithAName");
expect(lineNumber).toBe(518);
expect(parentLineNumber).toBe(523);
});
test("Error.prepareStackTrace inside a node:vm works", () => {
const { runInNewContext } = require("node:vm");
Error.prepareStackTrace = null;
const result = runInNewContext(
`
Error.prepareStackTrace = (err, stack) => {
if (typeof err.stack !== "string") {
throw new Error("err.stack is not a string");
}
return "custom stack trace";
};
const err = new Error();
err.stack;
`,
);
expect(result).toBe("custom stack trace");
expect(Error.prepareStackTrace).toBeNull();
});
test("Error.captureStackTrace inside error constructor works", () => {
class ExtendedError extends Error {
constructor() {
super();
Error.captureStackTrace(this, ExtendedError);
}
}
class AnotherError extends ExtendedError {}
expect(() => {
throw new AnotherError();
}).toThrow();
});
import "harness";
import { join } from "path";
test("Error.prepareStackTrace has a default implementation which behaves the same as being unset", () => {
expect([join(import.meta.dirname, "error-prepare-stack-default-fixture.js")]).toRun();
});
test("Error.prepareStackTrace returns a CallSite object", () => {
Error.prepareStackTrace = function (err, stack) {
return stack;
};
const error = new Error();
expect(error.stack[0]).not.toBeString();
expect(error.stack[0][Symbol.toStringTag]).toBe("CallSite");
});
test("Error.captureStackTrace updates the stack property each call, even if Error.prepareStackTrace is set", () => {
const prevPrepareStackTrace = Error.prepareStackTrace;
var didCallPrepareStackTrace = false;
let error = new Error();
const firstStack = error.stack;
Error.prepareStackTrace = function (err, stack) {
expect(err.stack).not.toBe(firstStack);
didCallPrepareStackTrace = true;
return stack;
};
function outer() {
inner();
}
function inner() {
Error.captureStackTrace(error);
}
outer();
const secondStack = error.stack;
expect(firstStack).not.toBe(secondStack);
expect(firstStack).toBeString();
expect(firstStack).not.toContain("outer");
expect(firstStack).not.toContain("inner");
expect(didCallPrepareStackTrace).toBe(true);
expect(secondStack.find(a => a.getFunctionName() === "outer")).toBeTruthy();
expect(secondStack.find(a => a.getFunctionName() === "inner")).toBeTruthy();
Error.prepareStackTrace = prevPrepareStackTrace;
});
test("Error.captureStackTrace updates the stack property each call", () => {
let error = new Error();
const firstStack = error.stack;
function outer() {
inner();
}
function inner() {
Error.captureStackTrace(error);
}
outer();
const secondStack = error.stack;
expect(firstStack).not.toBe(secondStack);
expect(firstStack.length).toBeLessThan(secondStack.length);
expect(firstStack).not.toContain("outer");
expect(firstStack).not.toContain("inner");
expect(secondStack).toContain("outer");
expect(secondStack).toContain("inner");
});
test("calling .stack later uses the stored StackTrace", function hey() {
let error = new Error();
let stack;
function outer() {
inner();
}
function inner() {
stack = error.stack;
}
outer();
expect(stack).not.toContain("outer");
expect(stack).not.toContain("inner");
expect(stack).toContain("hey");
});
test("calling .stack on a non-materialized Error updates the stack properly", function hey() {
let error = new Error();
let stack;
function outer() {
inner();
}
function inner() {
stack = error.stack;
}
function wrapped() {
Error.captureStackTrace(error);
}
wrapped();
outer();
expect(stack).not.toContain("outer");
expect(stack).not.toContain("inner");
expect(stack).toContain("hey");
expect(stack).toContain("wrapped");
});
test("Error.prepareStackTrace on an array with non-CallSite objects doesn't crash", () => {
const result = Error.prepareStackTrace(new Error("ok"), [{ a: 1 }, { b: 2 }, { c: 3 }]);
expect(result).toBe("Error: ok\n at [object Object]\n at [object Object]\n at [object Object]");
});
test("Error.prepareStackTrace calls toString()", () => {
const result = Error.prepareStackTrace(new Error("ok"), [
{ a: 1 },
{ b: 2 },
{
c: 3,
toString() {
return "potato";
},
},
]);
expect(result).toBe("Error: ok\n at [object Object]\n at [object Object]\n at potato");
});
test("Error.prepareStackTrace propagates exceptions", () => {
expect(() =>
Error.prepareStackTrace(new Error("ok"), [
{ a: 1 },
{ b: 2 },
{
c: 3,
toString() {
throw new Error("hi");
},
},
]),
).toThrow("hi");
});
test("CallFrame.p.getScriptNameOrSourceURL inside eval", () => {
let prevPrepareStackTrace = Error.prepareStackTrace;
const prepare = mock((e, s) => {
expect(s[0].getScriptNameOrSourceURL()).toBe("https://zombo.com/welcome-to-zombo.js");
expect(s[1].getScriptNameOrSourceURL()).toBe("https://zombo.com/welcome-to-zombo.js");
expect(s[2].getScriptNameOrSourceURL()).toBe("[native code]");
expect(s[3].getScriptNameOrSourceURL()).toBe(import.meta.path);
expect(s[4].getScriptNameOrSourceURL()).toBe(import.meta.path);
});
Error.prepareStackTrace = prepare;
let evalScript = `(function() {
throw new Error("bad error!");
})() //# sourceURL=https://zombo.com/welcome-to-zombo.js`;
try {
function insideAFunction() {
eval(evalScript);
}
insideAFunction();
} catch (e) {
e.stack;
}
Error.prepareStackTrace = prevPrepareStackTrace;
expect(prepare).toHaveBeenCalledTimes(1);
});
test("CallFrame.p.isAsync", async () => {
let prevPrepareStackTrace = Error.prepareStackTrace;
const prepare = mock((e, s) => {
expect(s[0].isAsync()).toBeFalse();
expect(s[1].isAsync()).toBeTrue();
expect(s[2].isAsync()).toBeTrue();
expect(s[3].isAsync()).toBeTrue();
});
Error.prepareStackTrace = prepare;
async function foo() {
await bar();
}
async function bar() {
await baz();
}
async function baz() {
await 1;
throw new Error("error from baz");
}
try {
await foo();
} catch (e) {
e.stack;
}
Error.prepareStackTrace = prevPrepareStackTrace;
expect(prepare).toHaveBeenCalledTimes(1);
});
test("captureStackTrace with constructor function not in stack returns error string", () => {
// When the second argument to captureStackTrace is a function that isn't in
// the call stack, all frames are filtered out and .stack should still return
// the error name and message (matching Node.js behavior).
function notInStack() {}
// Case 1: stack not accessed before captureStackTrace
{
const e = new Error("test");
Error.captureStackTrace(e, notInStack);
expect(e.stack).toBe("Error: test");
}
// Case 2: stack accessed before captureStackTrace
{
const e = new Error("test");
void e.stack;
Error.captureStackTrace(e, notInStack);
expect(e.stack).toBe("Error: test");
}
// Case 3: empty message
{
const e = new Error();
Error.captureStackTrace(e, notInStack);
expect(e.stack).toBe("Error");
}
// Case 4: custom error name
{
const e = new TypeError("bad type");
Error.captureStackTrace(e, notInStack);
expect(e.stack).toBe("TypeError: bad type");
}
});
// Regression test for https://github.com/oven-sh/bun/issues/27708
// Error.captureStackTrace must work with ES5-style Error subclasses
// even when Error.prepareStackTrace has been replaced (e.g. by @babel/core).
test("captureStackTrace works with ES5 Error subclass when prepareStackTrace is set", () => {
// ES5-style Error subclass (no super() call, just prototype chain setup)
// This is the pattern used by @xmldom/xmldom's ParseError
function ES5Error(message) {
this.message = message;
if (Error.captureStackTrace) Error.captureStackTrace(this, ES5Error);
}
ES5Error.prototype = Object.create(Error.prototype, {
constructor: { value: ES5Error },
name: { value: "ES5Error", enumerable: true },
});
// Works before prepareStackTrace is replaced
const e1 = new ES5Error("before");
expect(e1.message).toBe("before");
expect(typeof e1.stack).toBe("string");
expect(e1 instanceof Error).toBe(true);
// Replace prepareStackTrace with a wrapper (like @babel/core does)
const original = Error.prepareStackTrace;
Error.prepareStackTrace = function wrappedPrepareStackTrace(err, trace) {
// Babel's stackTraceRewriter chains to the original prepareStackTrace
if (original) return original(err, trace);
return `${err}\n at ${trace.join("\n at ")}`;
};
// Must still work after prepareStackTrace is replaced
const e2 = new ES5Error("after");
expect(e2.message).toBe("after");
expect(typeof e2.stack).toBe("string");
expect(e2 instanceof Error).toBe(true);
});
test("captureStackTrace works with plain object when prepareStackTrace is set", () => {
const original = Error.prepareStackTrace;
Error.prepareStackTrace = function wrappedPrepareStackTrace(err, trace) {
if (original) return original(err, trace);
return `${err}\n at ${trace.join("\n at ")}`;
};
// V8 allows any object as the target for captureStackTrace
const obj = { message: "plain object" };
Error.captureStackTrace(obj);
expect(typeof obj.stack).toBe("string");
});