Skip to content

Commit 35b08a8

Browse files
committed
remove traces helper, use Error.captureStackTrace instead
1 parent da03416 commit 35b08a8

File tree

3 files changed

+21
-57
lines changed

3 files changed

+21
-57
lines changed

src/renderStream/Render.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ As we only use this file in our internal tests, we can safely ignore it.
1111

1212
import { within, screen } from "@testing-library/dom";
1313
import { JSDOM, VirtualConsole } from "jsdom";
14-
import { applyStackTrace, captureStackTrace } from "./traces.js";
1514

1615
export interface BaseRender {
1716
id: string;
@@ -97,9 +96,8 @@ export class RenderInstance<Snapshot> implements Render<Snapshot> {
9796
}
9897

9998
const virtualConsole = new VirtualConsole();
100-
const stackTrace = captureStackTrace("RenderInstance.get");
10199
virtualConsole.on("jsdomError", (error: any) => {
102-
throw applyStackTrace(error, stackTrace);
100+
throw error;
103101
});
104102

105103
const snapDOM = new JSDOM(this.stringifiedDOM, {

src/renderStream/createRenderStream.tsx

+20-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as React from "rehackt";
22

33
import type { Render, BaseRender } from "./Render.js";
44
import { RenderInstance } from "./Render.js";
5-
import { applyStackTrace, captureStackTrace } from "./traces.js";
65
import type { RenderStreamContextValue } from "./context.js";
76
import {
87
RenderStreamContextProvider,
@@ -16,12 +15,8 @@ export type ValidSnapshot =
1615
| void
1716
| (object & { /* not a function */ call?: never });
1817

19-
/** only used for passing around data internally */
20-
const _stackTrace = Symbol();
21-
2218
export interface NextRenderOptions {
2319
timeout?: number;
24-
[_stackTrace]?: string;
2520
}
2621

2722
interface ReplaceSnapshot<Snapshot> {
@@ -263,10 +258,9 @@ export function createRenderStream<Snapshot extends ValidSnapshot = void>({
263258

264259
return render;
265260
}
266-
return stream.waitForNextRender({
267-
[_stackTrace]: captureStackTrace(stream.peekRender),
268-
...options,
269-
});
261+
return stream
262+
.waitForNextRender(options)
263+
.catch(rethrowWithCapturedStackTrace(stream.peekRender));
270264
},
271265
takeRender: markAssertable(async function takeRender(
272266
options: NextRenderOptions = {}
@@ -280,10 +274,12 @@ export function createRenderStream<Snapshot extends ValidSnapshot = void>({
280274

281275
try {
282276
return await stream.peekRender({
283-
[_stackTrace]: captureStackTrace(stream.takeRender),
284277
...options,
285278
});
286279
} catch (e) {
280+
if (e instanceof Object) {
281+
Error.captureStackTrace(e, stream.takeRender);
282+
}
287283
error = e;
288284
throw e;
289285
} finally {
@@ -313,11 +309,7 @@ export function createRenderStream<Snapshot extends ValidSnapshot = void>({
313309
}
314310
return render;
315311
},
316-
waitForNextRender({
317-
timeout = 1000,
318-
// capture the stack trace here so its stack trace is as close to the calling code as possible
319-
[_stackTrace]: stackTrace = captureStackTrace(stream.waitForNextRender),
320-
}: NextRenderOptions = {}) {
312+
waitForNextRender({ timeout = 1000 }: NextRenderOptions = {}) {
321313
if (!nextRender) {
322314
nextRender = Promise.race<Render<Snapshot>>([
323315
new Promise<Render<Snapshot>>((resolve, reject) => {
@@ -326,9 +318,9 @@ export function createRenderStream<Snapshot extends ValidSnapshot = void>({
326318
}),
327319
new Promise<Render<Snapshot>>((_, reject) =>
328320
setTimeout(() => {
329-
reject(
330-
applyStackTrace(new WaitForRenderTimeoutError(), stackTrace)
331-
);
321+
const error = new WaitForRenderTimeoutError();
322+
Error.captureStackTrace(error, stream.waitForNextRender);
323+
reject(error);
332324
resetNextRender();
333325
}, timeout)
334326
),
@@ -344,6 +336,7 @@ export function createRenderStream<Snapshot extends ValidSnapshot = void>({
344336
export class WaitForRenderTimeoutError extends Error {
345337
constructor() {
346338
super("Exceeded timeout waiting for next render.");
339+
this.name = "WaitForRenderTimeoutError";
347340
Object.setPrototypeOf(this, new.target.prototype);
348341
}
349342
}
@@ -381,3 +374,12 @@ export function useTrackRenders({ name }: { name?: string } = {}) {
381374
ctx.renderedComponents.unshift(component);
382375
});
383376
}
377+
378+
function rethrowWithCapturedStackTrace(constructorOpt: Function | undefined) {
379+
return function (error: unknown) {
380+
if (error instanceof Object) {
381+
Error.captureStackTrace(error, constructorOpt);
382+
}
383+
throw error;
384+
};
385+
}

src/renderStream/traces.ts

-36
This file was deleted.

0 commit comments

Comments
 (0)