Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions js/src/stackutil.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { expect, test } from "vitest";
import {
callerFromAnonymousFunction,
callerFromModuleInit,
callerFromNamedFunction,
} from "../tests/helpers/stackutil-caller";

function normalizeFileName(fileName: string) {
return fileName.replace(/^file:\/\//, "");
}

test("getCallerLocation works with a real top-level caller frame", () => {
expect(callerFromModuleInit).toBeDefined();
expect(callerFromModuleInit!.caller_lineno).toBeGreaterThan(1);
expect(normalizeFileName(callerFromModuleInit!.caller_filename)).toMatch(
/tests[\\/]helpers[\\/]stackutil-caller\.(ts|js)$/,
);
});

test("getCallerLocation works with a real named function caller frame", () => {
const location = callerFromNamedFunction();
expect(location).toBeDefined();
expect(location!.caller_lineno).toBeGreaterThan(1);
expect(normalizeFileName(location!.caller_filename)).toMatch(
/tests[\\/]helpers[\\/]stackutil-caller\.(ts|js)$/,
);
expect(location!.caller_functionname).toContain("callerFromNamedFunction");
});

test("getCallerLocation works with a real anonymous function caller frame", () => {
const location = callerFromAnonymousFunction();
expect(location).toBeDefined();
expect(location!.caller_lineno).toBeGreaterThan(1);
expect(normalizeFileName(location!.caller_filename)).toMatch(
/tests[\\/]helpers[\\/]stackutil-caller\.(ts|js)$/,
);
});
4 changes: 2 additions & 2 deletions js/src/stackutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ function getStackTrace(): StackTraceEntry[] {
}
const traceLines = trace.split("\n");
const out: StackTraceEntry[] = [];
const stackFrameRegex = /at(.*)\((.*):(\d+):(\d+)\)/;
const stackFrameRegex = /^\s*at\s+(?:(.+?)\s+\()?(.*):(\d+):(\d+)\)?\s*$/;
for (const traceLine of traceLines.slice(1)) {
const matches = traceLine.match(stackFrameRegex);
if (matches === null || matches.length !== 5) {
continue;
}
const entry: StackTraceEntry = {
functionName: matches[1].trim(),
functionName: matches[1]?.trim() ?? "",
fileName: matches[2],
lineNo: parseInt(matches[3]),
};
Expand Down
14 changes: 14 additions & 0 deletions js/tests/helpers/stackutil-caller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { configureNode } from "../../src/node/config";
import { getCallerLocation } from "../../src/stackutil";

configureNode();

export const callerFromModuleInit = getCallerLocation();

export function callerFromNamedFunction() {
return getCallerLocation();
}

export function callerFromAnonymousFunction() {
return (() => getCallerLocation())();
}
Loading