chore: render original exception location in error frame (#16515)

This commit is contained in:
Pavel Feldman 2022-08-15 09:28:55 -07:00 committed by GitHub
parent 9f22ae9aa9
commit bd06d1604f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 15 deletions

View File

@ -375,7 +375,9 @@ export function formatError(config: FullConfig, error: TestError, highlightCode:
const tokens = []; const tokens = [];
let location: Location | undefined; let location: Location | undefined;
if (stack) { 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); tokens.push(parsed.message);
location = parsed.location; location = parsed.location;
if (location) { if (location) {
@ -416,15 +418,11 @@ function indent(lines: string, tab: string) {
return lines.replace(/^(?=.+$)/gm, tab); return lines.replace(/^(?=.+$)/gm, tab);
} }
export function prepareErrorStack(stack: string, file?: string): { export function prepareErrorStack(stack: string): {
message: string; message: string;
stackLines: string[]; stackLines: string[];
location?: Location; 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'); const lines = stack.split('\n');
let firstStackLine = lines.findIndex(line => line.startsWith(' at ')); let firstStackLine = lines.findIndex(line => line.startsWith(' at '));
if (firstStackLine === -1) if (firstStackLine === -1)
@ -436,11 +434,9 @@ export function prepareErrorStack(stack: string, file?: string): {
const { frame: parsed, fileName: resolvedFile } = parseStackTraceLine(line); const { frame: parsed, fileName: resolvedFile } = parseStackTraceLine(line);
if (!parsed || !resolvedFile) if (!parsed || !resolvedFile)
continue; continue;
if (!file || resolvedFile === file) {
location = { file: resolvedFile, column: parsed.column || 0, line: parsed.line || 0 }; location = { file: resolvedFile, column: parsed.column || 0, line: parsed.line || 0 };
break; break;
} }
}
return { message, stackLines, location }; return { message, stackLines, location };
} }

View File

@ -185,7 +185,7 @@ class JSONReporter implements Reporter {
})), })),
}; };
if (result.error?.stack) if (result.error?.stack)
jsonResult.errorLocation = prepareErrorStack(result.error.stack, test.location.file).location; jsonResult.errorLocation = prepareErrorStack(result.error.stack).location;
return jsonResult; return jsonResult;
} }

View File

@ -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');`); 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({ const result = await runInlineTest({
'helper.ts': ` 'helper.ts': `
export function ohMy() { export function ohMy() {
@ -105,8 +105,8 @@ test('should not print codeframe from a helper', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(1); expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1); expect(result.failed).toBe(1);
expect(result.output).toContain('Error: oh my'); expect(result.output).toContain('Error: oh my');
expect(result.output).toContain(` 7 | test('foobar', async ({}) => {`); expect(result.output).toContain(` 4 | export function ohMy() {`);
expect(result.output).toContain(`> 8 | ohMy();`); expect(result.output).toContain(` > 5 | throw new Error('oh my');`);
expect(result.output).toContain(` | ^`); expect(result.output).toContain(` | ^`);
}); });