Skip to content

Commit acdc496

Browse files
committed
implement review suggestions
1 parent 5430b64 commit acdc496

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

src/mocks/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const healthHandler = http.get("*/health", () => {
3030
const httpServer = createServer(healthHandler, ...handlers);
3131

3232
httpServer.on("request", (req: IncomingMessage, _res: ServerResponse) => {
33+
// Skip logging health checks to reduce CI noise
34+
if (req.url?.includes("/health")) return;
3335
console.log(`[mock] ${req.method} ${req.url}`);
3436
});
3537

tests/bdd/steps/global.steps.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import { injectAuthCookies } from "../support/auth.ts";
44
import type { PlaywrightWorld } from "../support/world";
55

66
Given("I am on {string}", async function (this: PlaywrightWorld, path: string) {
7-
await this.page.goto(`${this.baseUrl}${path}`);
7+
await this.requirePage().goto(`${this.baseUrl}${path}`);
88
});
99

1010
Given("I am logged in", async function (this: PlaywrightWorld) {
1111
// Perform login in a separate page and inject cookies into context
12-
await injectAuthCookies(this.context);
12+
await injectAuthCookies(this.requireContext());
1313
});
1414

1515
// Generic click step using the {role} parameter type (canonical phrases only)
1616
When(
1717
"I click on the {string} {role}",
1818
async function (this: PlaywrightWorld, label: string, role: AriaRole) {
19-
await this.page.getByRole(role, { name: label }).click();
19+
await this.requirePage().getByRole(role, { name: label }).click();
2020
},
2121
);
2222

@@ -25,23 +25,23 @@ When(
2525
Then(
2626
"I should see the text {string}",
2727
async function (this: PlaywrightWorld, text: string) {
28-
await expect(this.page.getByText(text)).toBeVisible();
28+
await expect(this.requirePage().getByText(text)).toBeVisible();
2929
},
3030
);
3131

3232
Then(
3333
"I should see a heading {string}",
3434
async function (this: PlaywrightWorld, heading: string) {
3535
await expect(
36-
this.page.getByRole("heading", { name: heading }),
36+
this.requirePage().getByRole("heading", { name: heading }),
3737
).toBeVisible();
3838
},
3939
);
4040

4141
Then(
4242
"I should be on {string}",
4343
async function (this: PlaywrightWorld, path: string) {
44-
await expect(this.page).toHaveURL(
44+
await expect(this.requirePage()).toHaveURL(
4545
new RegExp(
4646
`${this.baseUrl}${path.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&")}$`,
4747
),

tests/bdd/support/hooks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ Before(async function (this: PlaywrightWorld) {
5050
After(async function (this: PlaywrightWorld, scenario: ITestCaseHookParameter) {
5151
if (TRACE_ENABLED && this.context) {
5252
const safeName = scenario.pickle.name.replace(/[^a-z0-9-]+/gi, "_");
53+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
5354
const { mkdir } = await import("node:fs/promises");
5455
await mkdir("test-results/traces", { recursive: true });
5556
await this.context.tracing.stop({
56-
path: `test-results/traces/${safeName}.zip`,
57+
path: `test-results/traces/${safeName}_${timestamp}.zip`,
5758
});
5859
}
5960
if (this.page) await this.page.close();

tests/bdd/support/world.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@ import { setWorldConstructor, type World } from "@cucumber/cucumber";
22
import type { BrowserContext, Page } from "@playwright/test";
33

44
export class PlaywrightWorld implements World {
5-
page!: Page;
6-
context!: BrowserContext;
5+
page: Page | undefined;
6+
context: BrowserContext | undefined;
77
baseUrl: string;
88

99
constructor() {
1010
this.baseUrl = process.env.BASE_URL || "http://localhost:3000";
1111
}
12+
13+
requirePage(): Page {
14+
if (!this.page)
15+
throw new Error("Page not initialized - Before hook may have failed");
16+
return this.page;
17+
}
18+
19+
requireContext(): BrowserContext {
20+
if (!this.context)
21+
throw new Error("Context not initialized - Before hook may have failed");
22+
return this.context;
23+
}
1224
}
1325

1426
setWorldConstructor(PlaywrightWorld);

0 commit comments

Comments
 (0)