chore: do not point to node_modules in code frames (#18358)

Fixes https://github.com/microsoft/playwright/issues/18330
This commit is contained in:
Pavel Feldman 2022-10-26 15:18:31 -07:00 committed by GitHub
parent 1505a952fe
commit db456a020c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -439,6 +439,8 @@ export function prepareErrorStack(stack: string): {
const { frame: parsed, fileName: resolvedFile } = parseStackTraceLine(line);
if (!parsed || !resolvedFile)
continue;
if (belongsToNodeModules(resolvedFile))
continue;
location = { file: resolvedFile, column: parsed.column || 0, line: parsed.line || 0 };
break;
}
@ -481,3 +483,7 @@ function fitToWidth(line: string, width: number, prefix?: string): string {
}
return taken.reverse().join('');
}
function belongsToNodeModules(file: string) {
return file.includes(`${path.sep}node_modules${path.sep}`);
}

View File

@ -85,6 +85,35 @@ test('should print an error in a codeframe', async ({ runInlineTest }) => {
expect(result.output).toContain(`> 7 | const error = new Error('my-message');`);
});
test('should filter out node_modules error in a codeframe', async ({ runInlineTest }) => {
const result = await runInlineTest({
'node_modules/utils/utils.js': `
function assert(value) {
if (!value)
throw new Error('Assertion error');
}
module.exports = { assert };
`,
'a.spec.ts': `
const { test } = pwt;
const { assert } = require('utils/utils.js');
test('fail', async ({}) => {
assert(false);
});
`
});
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1);
const output = stripAnsi(result.output);
expect(output).toContain('Error: Assertion error');
expect(output).toContain('a.spec.ts:7:7 fail');
expect(output).toContain(` 7 | test('fail', async ({}) => {`);
expect(output).toContain(`> 8 | assert(false);`);
expect(output).toContain(` | ^`);
expect(output).toContain(`utils.js:6`);
expect(output).toContain(`a.spec.ts:8:9`);
});
test('should print codeframe from a helper', async ({ runInlineTest }) => {
const result = await runInlineTest({
'helper.ts': `