From 7f9c2f9e3509c0986a17a838592129a0b07392db Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez Date: Sun, 28 Dec 2025 17:14:54 +0200 Subject: [PATCH] fix: match tests by title instead of line number in reporter Playwright's `test.location.line` in reporters may point to the describe block instead of the actual test line. This causes "Cannot find bddTestData" errors in the cucumber reporter because the line number doesn't match the `pwTestLine` in bddFileData. This fix adds the test title to bddFileData and uses it as the primary matching criterion, with line number as a fallback for backward compatibility. Changes: - Add `testTitle` field to BddTestData type - Store test title in BddDataRenderer during generation - Match by test title first, then by line number in MessagesBuilder --- src/bddData/renderer.ts | 1 + src/bddData/types.ts | 1 + src/reporter/cucumber/messagesBuilder/index.ts | 12 +++++++++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/bddData/renderer.ts b/src/bddData/renderer.ts index c0320f28..b7e8868e 100644 --- a/src/bddData/renderer.ts +++ b/src/bddData/renderer.ts @@ -51,6 +51,7 @@ export class BddDataRenderer { return { pwTestLine: this.sourceMapper.getPwTestLine(test.pickle), pickleLine: test.pickle.location.line, + testTitle: test.testTitle, // store test title for reliable matching in reporter skipped: test.skipped || undefined, timeout: test.ownTimeout, slow: test.slow || undefined, diff --git a/src/bddData/types.ts b/src/bddData/types.ts index 496e7f21..165d3cdd 100644 --- a/src/bddData/types.ts +++ b/src/bddData/types.ts @@ -17,6 +17,7 @@ export type BddFileData = BddTestData[]; export type BddTestData = { pwTestLine: number; pickleLine: number; + testTitle: string; // test title for reliable matching in reporter tags: string[]; skipped?: boolean; timeout?: number; diff --git a/src/reporter/cucumber/messagesBuilder/index.ts b/src/reporter/cucumber/messagesBuilder/index.ts index dcb38e3a..d4098176 100644 --- a/src/reporter/cucumber/messagesBuilder/index.ts +++ b/src/reporter/cucumber/messagesBuilder/index.ts @@ -63,11 +63,17 @@ export class MessagesBuilder { if (!bddConfig) return; const { bddData, featureUri } = this.testFiles.getBddData(test.location.file); - // todo: move these line somewhere else - const bddTestData = bddData.find((data) => data.pwTestLine === test.location.line); + // Match by test title (reliable) with fallback to line number (legacy). + // Note: Playwright's test.location.line in reporters may point to describe block + // instead of actual test line, so we prefer matching by title. + const bddTestData = + bddData.find((data) => data.testTitle === test.title) || + bddData.find((data) => data.pwTestLine === test.location.line); if (!bddTestData) { const filePath = relativeToCwd(test.location.file); - throw new Error(`Cannot find bddTestData for ${filePath}:${test.location.line}`); + throw new Error( + `Cannot find bddTestData for ${filePath}:${test.location.line} (title: ${test.title})`, + ); } // Important to create TestCaseRun in this method (not later),