Skip to content

Commit

Permalink
remove traces helper, use Error.captureStackTrace instead
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Oct 10, 2024
1 parent da03416 commit 35b08a8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 57 deletions.
4 changes: 1 addition & 3 deletions src/renderStream/Render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ As we only use this file in our internal tests, we can safely ignore it.

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

export interface BaseRender {
id: string;
Expand Down Expand Up @@ -97,9 +96,8 @@ export class RenderInstance<Snapshot> implements Render<Snapshot> {
}

const virtualConsole = new VirtualConsole();
const stackTrace = captureStackTrace("RenderInstance.get");
virtualConsole.on("jsdomError", (error: any) => {
throw applyStackTrace(error, stackTrace);
throw error;
});

const snapDOM = new JSDOM(this.stringifiedDOM, {
Expand Down
38 changes: 20 additions & 18 deletions src/renderStream/createRenderStream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as React from "rehackt";

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

/** only used for passing around data internally */
const _stackTrace = Symbol();

export interface NextRenderOptions {
timeout?: number;
[_stackTrace]?: string;
}

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

return render;
}
return stream.waitForNextRender({
[_stackTrace]: captureStackTrace(stream.peekRender),
...options,
});
return stream
.waitForNextRender(options)
.catch(rethrowWithCapturedStackTrace(stream.peekRender));
},
takeRender: markAssertable(async function takeRender(
options: NextRenderOptions = {}
Expand All @@ -280,10 +274,12 @@ export function createRenderStream<Snapshot extends ValidSnapshot = void>({

try {
return await stream.peekRender({
[_stackTrace]: captureStackTrace(stream.takeRender),
...options,
});
} catch (e) {
if (e instanceof Object) {
Error.captureStackTrace(e, stream.takeRender);
}
error = e;
throw e;
} finally {
Expand Down Expand Up @@ -313,11 +309,7 @@ export function createRenderStream<Snapshot extends ValidSnapshot = void>({
}
return render;
},
waitForNextRender({
timeout = 1000,
// capture the stack trace here so its stack trace is as close to the calling code as possible
[_stackTrace]: stackTrace = captureStackTrace(stream.waitForNextRender),
}: NextRenderOptions = {}) {
waitForNextRender({ timeout = 1000 }: NextRenderOptions = {}) {
if (!nextRender) {
nextRender = Promise.race<Render<Snapshot>>([
new Promise<Render<Snapshot>>((resolve, reject) => {
Expand All @@ -326,9 +318,9 @@ export function createRenderStream<Snapshot extends ValidSnapshot = void>({
}),
new Promise<Render<Snapshot>>((_, reject) =>
setTimeout(() => {
reject(
applyStackTrace(new WaitForRenderTimeoutError(), stackTrace)
);
const error = new WaitForRenderTimeoutError();
Error.captureStackTrace(error, stream.waitForNextRender);
reject(error);
resetNextRender();
}, timeout)
),
Expand All @@ -344,6 +336,7 @@ export function createRenderStream<Snapshot extends ValidSnapshot = void>({
export class WaitForRenderTimeoutError extends Error {
constructor() {
super("Exceeded timeout waiting for next render.");
this.name = "WaitForRenderTimeoutError";
Object.setPrototypeOf(this, new.target.prototype);
}
}
Expand Down Expand Up @@ -381,3 +374,12 @@ export function useTrackRenders({ name }: { name?: string } = {}) {
ctx.renderedComponents.unshift(component);
});
}

function rethrowWithCapturedStackTrace(constructorOpt: Function | undefined) {
return function (error: unknown) {
if (error instanceof Object) {
Error.captureStackTrace(error, constructorOpt);
}
throw error;
};
}
36 changes: 0 additions & 36 deletions src/renderStream/traces.ts

This file was deleted.

0 comments on commit 35b08a8

Please sign in to comment.