refactor(test): share pw-tools-core test setup

This commit is contained in:
Peter Steinberger
2026-02-14 20:54:51 +00:00
parent 20cefd78cb
commit 09fa33f7e2
5 changed files with 139 additions and 209 deletions

View File

@@ -1,54 +1,18 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { describe, expect, it, vi } from "vitest";
import {
installPwToolsCoreTestHooks,
setPwToolsCoreCurrentPage,
setPwToolsCoreCurrentRefLocator,
} from "./pw-tools-core.test-harness.js";
let currentPage: Record<string, unknown> | null = null;
let currentRefLocator: Record<string, unknown> | null = null;
let pageState: {
console: unknown[];
armIdUpload: number;
armIdDialog: number;
armIdDownload: number;
};
const sessionMocks = vi.hoisted(() => ({
getPageForTargetId: vi.fn(async () => {
if (!currentPage) {
throw new Error("missing page");
}
return currentPage;
}),
ensurePageState: vi.fn(() => pageState),
restoreRoleRefsForTarget: vi.fn(() => {}),
refLocator: vi.fn(() => {
if (!currentRefLocator) {
throw new Error("missing locator");
}
return currentRefLocator;
}),
rememberRoleRefsForTarget: vi.fn(() => {}),
}));
vi.mock("./pw-session.js", () => sessionMocks);
installPwToolsCoreTestHooks();
const mod = await import("./pw-tools-core.js");
describe("pw-tools-core", () => {
beforeEach(() => {
currentPage = null;
currentRefLocator = null;
pageState = {
console: [],
armIdUpload: 0,
armIdDialog: 0,
armIdDownload: 0,
};
for (const fn of Object.values(sessionMocks)) {
fn.mockClear();
}
});
it("clamps timeoutMs for scrollIntoView", async () => {
const scrollIntoViewIfNeeded = vi.fn(async () => {});
currentRefLocator = { scrollIntoViewIfNeeded };
currentPage = {};
setPwToolsCoreCurrentRefLocator({ scrollIntoViewIfNeeded });
setPwToolsCoreCurrentPage({});
await mod.scrollIntoViewViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
@@ -63,8 +27,8 @@ describe("pw-tools-core", () => {
const scrollIntoViewIfNeeded = vi.fn(async () => {
throw new Error('Error: strict mode violation: locator("aria-ref=1") resolved to 2 elements');
});
currentRefLocator = { scrollIntoViewIfNeeded };
currentPage = {};
setPwToolsCoreCurrentRefLocator({ scrollIntoViewIfNeeded });
setPwToolsCoreCurrentPage({});
await expect(
mod.scrollIntoViewViaPlaywright({
@@ -78,8 +42,8 @@ describe("pw-tools-core", () => {
const scrollIntoViewIfNeeded = vi.fn(async () => {
throw new Error('Timeout 5000ms exceeded. waiting for locator("aria-ref=1") to be visible');
});
currentRefLocator = { scrollIntoViewIfNeeded };
currentPage = {};
setPwToolsCoreCurrentRefLocator({ scrollIntoViewIfNeeded });
setPwToolsCoreCurrentPage({});
await expect(
mod.scrollIntoViewViaPlaywright({
@@ -93,8 +57,8 @@ describe("pw-tools-core", () => {
const click = vi.fn(async () => {
throw new Error('Error: strict mode violation: locator("aria-ref=1") resolved to 2 elements');
});
currentRefLocator = { click };
currentPage = {};
setPwToolsCoreCurrentRefLocator({ click });
setPwToolsCoreCurrentPage({});
await expect(
mod.clickViaPlaywright({
@@ -108,8 +72,8 @@ describe("pw-tools-core", () => {
const click = vi.fn(async () => {
throw new Error('Timeout 5000ms exceeded. waiting for locator("aria-ref=1") to be visible');
});
currentRefLocator = { click };
currentPage = {};
setPwToolsCoreCurrentRefLocator({ click });
setPwToolsCoreCurrentPage({});
await expect(
mod.clickViaPlaywright({
@@ -125,8 +89,8 @@ describe("pw-tools-core", () => {
"Element is not receiving pointer events because another element intercepts pointer events",
);
});
currentRefLocator = { click };
currentPage = {};
setPwToolsCoreCurrentRefLocator({ click });
setPwToolsCoreCurrentPage({});
await expect(
mod.clickViaPlaywright({