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),