From db456a020c143fd14a0d7f5cf9a8d23da87df44a Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Wed, 26 Oct 2022 15:18:31 -0700 Subject: [PATCH] chore: do not point to node_modules in code frames (#18358) Fixes https://github.com/microsoft/playwright/issues/18330 --- .../playwright-test/src/reporters/base.ts | 6 ++++ tests/playwright-test/reporter-base.spec.ts | 29 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/packages/playwright-test/src/reporters/base.ts b/packages/playwright-test/src/reporters/base.ts index c71cc919e2..e74e06194e 100644 --- a/packages/playwright-test/src/reporters/base.ts +++ b/packages/playwright-test/src/reporters/base.ts @@ -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}`); +} diff --git a/tests/playwright-test/reporter-base.spec.ts b/tests/playwright-test/reporter-base.spec.ts index d044c1a8a2..3377a6ffa2 100644 --- a/tests/playwright-test/reporter-base.spec.ts +++ b/tests/playwright-test/reporter-base.spec.ts @@ -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': `