Skip to content
Closed
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
33 changes: 23 additions & 10 deletions packages/isomorphic/stackTrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ export type StackFrame = {

export function captureRawStack(): RawStack {
const stackTraceLimit = Error.stackTraceLimit;
Error.stackTraceLimit = 50;
const error = new Error();
const stack = error.stack || '';
Error.stackTraceLimit = 200;
const obj: { stack?: string } = {};
Error.captureStackTrace(obj);
const stack = obj.stack || '';
Error.stackTraceLimit = stackTraceLimit;
return stack.split('\n');
}
Expand Down Expand Up @@ -107,11 +108,17 @@ export function parseStackFrame(text: string, pathSeparator: string, showInterna
}

export function rewriteErrorMessage<E extends Error>(e: E, newMessage: string): E {
const lines: string[] = (e.stack?.split('\n') || []).filter(l => l.startsWith(' at '));
const originalStack = e.stack || '';

e.message = newMessage;

const errorTitle = `${e.name}: ${e.message}`;
if (lines.length)
e.stack = `${errorTitle}\n${lines.join('\n')}`;

if (originalStack) {
const stackWithoutTitle = originalStack.split('\n').slice(1).join('\n');
e.stack = `${errorTitle}\n${stackWithoutTitle}`;
}

return e;
}

Expand Down Expand Up @@ -150,10 +157,16 @@ export function parseErrorStack(stack: string, pathSeparator: string, showIntern
const frame = parseStackFrame(line, pathSeparator, showInternalStackFrames);
if (!frame || !frame.file)
continue;
if (belongsToNodeModules(frame.file, pathSeparator))
continue;
location = { file: frame.file, column: frame.column || 0, line: frame.line || 0 };
break;
// Prefer non-node_modules frames
if (!belongsToNodeModules(frame.file, pathSeparator)) {
location = { file: frame.file, column: frame.column || 0, line: frame.line || 0 };
break;
}

// fallback if only node_modules found
if (!location) {
location = { file: frame.file, column: frame.column || 0, line: frame.line || 0 };
}
}
return { message, stackLines, location };
}
Expand Down
11 changes: 7 additions & 4 deletions packages/playwright/src/worker/testInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,6 @@ export class TestInfoImpl implements TestInfo {
if (typeof result.error === 'object' && !(result.error as any)?.[stepSymbol])
(result.error as any)[stepSymbol] = step;
const error = testInfoError(result.error);
if (step.boxedStack)
error.stack = `${error.message}\n${stringifyStackFrames(step.boxedStack).join('\n')}`;
step.error = error;
}

Expand Down Expand Up @@ -410,8 +408,13 @@ export class TestInfoImpl implements TestInfo {
this.status = error instanceof TimeoutManagerError ? 'timedOut' : 'failed';
const serialized = testInfoError(error);
const step: TestStepInternal | undefined = typeof error === 'object' ? (error as any)?.[stepSymbol] : undefined;
if (step && step.boxedStack)
serialized.stack = `${(error as Error).name}: ${(error as Error).message}\n${stringifyStackFrames(step.boxedStack).join('\n')}`;

if (step && step.boxedStack) {
const originalStack = (error as Error).stack || `${(error as Error).name}: ${(error as Error).message}`;
const stepStack = stringifyStackFrames(step.boxedStack).join('\n');

serialized.stack = `${originalStack}\n--- STEP ---\n${stepStack}`;
}
this.errors.push(serialized);
this._tracing.appendForError(serialized);
}
Expand Down