mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: render original exception location in error frame (#16515)
This commit is contained in:
parent
9f22ae9aa9
commit
bd06d1604f
@ -375,7 +375,9 @@ export function formatError(config: FullConfig, error: TestError, highlightCode:
|
||||
const tokens = [];
|
||||
let location: Location | undefined;
|
||||
if (stack) {
|
||||
const parsed = prepareErrorStack(stack, file);
|
||||
// Now that we filter out internals from our stack traces, we can safely render
|
||||
// the helper / original exception locations.
|
||||
const parsed = prepareErrorStack(stack);
|
||||
tokens.push(parsed.message);
|
||||
location = parsed.location;
|
||||
if (location) {
|
||||
@ -416,15 +418,11 @@ function indent(lines: string, tab: string) {
|
||||
return lines.replace(/^(?=.+$)/gm, tab);
|
||||
}
|
||||
|
||||
export function prepareErrorStack(stack: string, file?: string): {
|
||||
export function prepareErrorStack(stack: string): {
|
||||
message: string;
|
||||
stackLines: string[];
|
||||
location?: Location;
|
||||
} {
|
||||
if (file) {
|
||||
// Stack will have /private/var/folders instead of /var/folders on Mac.
|
||||
file = fs.realpathSync(file);
|
||||
}
|
||||
const lines = stack.split('\n');
|
||||
let firstStackLine = lines.findIndex(line => line.startsWith(' at '));
|
||||
if (firstStackLine === -1)
|
||||
@ -436,10 +434,8 @@ export function prepareErrorStack(stack: string, file?: string): {
|
||||
const { frame: parsed, fileName: resolvedFile } = parseStackTraceLine(line);
|
||||
if (!parsed || !resolvedFile)
|
||||
continue;
|
||||
if (!file || resolvedFile === file) {
|
||||
location = { file: resolvedFile, column: parsed.column || 0, line: parsed.line || 0 };
|
||||
break;
|
||||
}
|
||||
location = { file: resolvedFile, column: parsed.column || 0, line: parsed.line || 0 };
|
||||
break;
|
||||
}
|
||||
return { message, stackLines, location };
|
||||
}
|
||||
|
||||
@ -185,7 +185,7 @@ class JSONReporter implements Reporter {
|
||||
})),
|
||||
};
|
||||
if (result.error?.stack)
|
||||
jsonResult.errorLocation = prepareErrorStack(result.error.stack, test.location.file).location;
|
||||
jsonResult.errorLocation = prepareErrorStack(result.error.stack).location;
|
||||
return jsonResult;
|
||||
}
|
||||
|
||||
|
||||
@ -85,7 +85,7 @@ test('should print an error in a codeframe', async ({ runInlineTest }) => {
|
||||
expect(result.output).toContain(`> 7 | const error = new Error('my-message');`);
|
||||
});
|
||||
|
||||
test('should not print codeframe from a helper', async ({ runInlineTest }) => {
|
||||
test('should print codeframe from a helper', async ({ runInlineTest }) => {
|
||||
const result = await runInlineTest({
|
||||
'helper.ts': `
|
||||
export function ohMy() {
|
||||
@ -105,9 +105,9 @@ test('should not print codeframe from a helper', async ({ runInlineTest }) => {
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.failed).toBe(1);
|
||||
expect(result.output).toContain('Error: oh my');
|
||||
expect(result.output).toContain(` 7 | test('foobar', async ({}) => {`);
|
||||
expect(result.output).toContain(`> 8 | ohMy();`);
|
||||
expect(result.output).toContain(` | ^`);
|
||||
expect(result.output).toContain(` 4 | export function ohMy() {`);
|
||||
expect(result.output).toContain(` > 5 | throw new Error('oh my');`);
|
||||
expect(result.output).toContain(` | ^`);
|
||||
});
|
||||
|
||||
test('should print slow tests', async ({ runInlineTest }) => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user