Skip to content

Commit

Permalink
fix empty step locations for esm
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalets committed Nov 22, 2023
1 parent 6077128 commit 7f124ad
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/playwright/getLocationInFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions test/esm-ts/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
16 changes: 16 additions & 0 deletions test/esm-ts/reporter.ts
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 6 additions & 1 deletion test/esm-ts/test.mjs
Original file line number Diff line number Diff line change
@@ -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`,
);
});
5 changes: 2 additions & 3 deletions test/report/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}

Expand Down

0 comments on commit 7f124ad

Please sign in to comment.