From 7f124add8983e0d549a0684b99a2b0dee3ec873c Mon Sep 17 00:00:00 2001 From: Vitaliy Potapov Date: Wed, 22 Nov 2023 14:39:28 +0400 Subject: [PATCH] fix empty step locations for esm --- CHANGELOG.md | 3 +++ src/playwright/getLocationInFile.ts | 9 ++++++--- test/esm-ts/playwright.config.ts | 1 + test/esm-ts/reporter.ts | 16 ++++++++++++++++ test/esm-ts/test.mjs | 7 ++++++- test/report/reporter.ts | 5 ++--- 6 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 test/esm-ts/reporter.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 80594f82..6afc5a5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## dev +* fix empty step locations for esm + ## 5.5.0 * add support for hooks, [#15](https://github.com/vitalets/playwright-bdd/issues/15) * add support for custom fixtures in cucumber-style steps diff --git a/src/playwright/getLocationInFile.ts b/src/playwright/getLocationInFile.ts index d608e108..df25c66b 100644 --- a/src/playwright/getLocationInFile.ts +++ b/src/playwright/getLocationInFile.ts @@ -13,16 +13,19 @@ interface Location { * See: https://github.com/microsoft/playwright/blob/main/packages/playwright-test/src/common/transform.ts#L229 */ export function getLocationInFile(filePath: string) { + const filePathUrl = url.pathToFileURL(filePath).toString(); const { sourceMapSupport } = requirePlaywrightModule('lib/utilsBundle.js'); const oldPrepareStackTrace = Error.prepareStackTrace; Error.prepareStackTrace = (error, stackFrames) => { - const frameInFile = stackFrames.find((frame) => frame.getFileName() === filePath); + const frameInFile = stackFrames.find((frame) => { + const frameFile = frame.getFileName(); + return frameFile === filePath || frameFile === filePathUrl; + }); if (!frameInFile) return { file: '', line: 0, column: 0 }; const frame: NodeJS.CallSite = sourceMapSupport.wrapCallSite(frameInFile); const fileName = frame.getFileName(); // Node error stacks for modules use file:// urls instead of paths. - const file = - fileName && fileName.startsWith('file://') ? url.fileURLToPath(fileName) : fileName; + const file = fileName?.startsWith('file://') ? url.fileURLToPath(fileName) : fileName; return { file, line: frame.getLineNumber(), diff --git a/test/esm-ts/playwright.config.ts b/test/esm-ts/playwright.config.ts index a79d9084..ea45d3ac 100644 --- a/test/esm-ts/playwright.config.ts +++ b/test/esm-ts/playwright.config.ts @@ -2,6 +2,7 @@ import { defineConfig } from '@playwright/test'; import { defineBddConfig } from '../../dist/index.js'; export default defineConfig({ + reporter: [['./reporter.ts']], projects: [ { name: 'project one', diff --git a/test/esm-ts/reporter.ts b/test/esm-ts/reporter.ts new file mode 100644 index 00000000..52108e18 --- /dev/null +++ b/test/esm-ts/reporter.ts @@ -0,0 +1,16 @@ +/** + * Custom reporter that prints step info to stdout. + */ +import path from 'node:path'; +import { Reporter, TestCase, TestResult, TestStep } from '@playwright/test/reporter'; + +class MyReporter implements Reporter { + onStepEnd(test: TestCase, result: TestResult, { title, location }: TestStep) { + const file = location?.file; + const relFile = file ? path.relative(process.cwd(), file) : ''; + // eslint-disable-next-line no-console + console.log(title, `${relFile}:${location?.line || 0}:${location?.column || 0}`); + } +} + +export default MyReporter; diff --git a/test/esm-ts/test.mjs b/test/esm-ts/test.mjs index 309a921e..ee441ba4 100644 --- a/test/esm-ts/test.mjs +++ b/test/esm-ts/test.mjs @@ -1,8 +1,13 @@ +import { normalize } from 'node:path'; +import { expect } from '@playwright/test'; import { test, getTestName, execPlaywrightTest, DEFAULT_CMD } from '../helpers.mjs'; test(getTestName(import.meta), (t) => { - execPlaywrightTest( + const stdout = execPlaywrightTest( t.name, `npx cross-env NODE_OPTIONS="--loader ts-node/esm --no-warnings" ${DEFAULT_CMD}`, ); + expect(stdout).toContain( + `Given State 1 ${normalize('.features-gen/one/sample.feature.spec.js')}:7:11`, + ); }); diff --git a/test/report/reporter.ts b/test/report/reporter.ts index 22d06a53..52108e18 100644 --- a/test/report/reporter.ts +++ b/test/report/reporter.ts @@ -7,10 +7,9 @@ import { Reporter, TestCase, TestResult, TestStep } from '@playwright/test/repor class MyReporter implements Reporter { onStepEnd(test: TestCase, result: TestResult, { title, location }: TestStep) { const file = location?.file; - if (!file) return; - const relFile = path.relative(process.cwd(), file); + const relFile = file ? path.relative(process.cwd(), file) : ''; // eslint-disable-next-line no-console - console.log(title, `${relFile}:${location.line}:${location.column}`); + console.log(title, `${relFile}:${location?.line || 0}:${location?.column || 0}`); } }