From 243c1385a09148c0106aa791c130c37102cc7f54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=AF=BC=EC=9E=AC?= Date: Mon, 25 May 2026 07:15:27 +0900 Subject: [PATCH] feat(js): export buildContextOptions helper (#262) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 이민재 <19909783+honor2030@users.noreply.github.com> --- js/src/index.ts | 2 +- js/src/playwright.ts | 36 ++++++++++++++++++++++-------------- js/tests/launch.test.ts | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 16 deletions(-) diff --git a/js/src/index.ts b/js/src/index.ts index d72882f..f824f1b 100644 --- a/js/src/index.ts +++ b/js/src/index.ts @@ -16,7 +16,7 @@ */ // Launch functions (Playwright API) -export { launch, launchContext, launchPersistentContext, buildLaunchOptions, humanizeBrowser } from "./playwright.js"; +export { launch, launchContext, launchPersistentContext, buildLaunchOptions, buildContextOptions, humanizeBrowser } from "./playwright.js"; // Binary management export { ensureBinary, clearCache, binaryInfo, checkForUpdate } from "./download.js"; diff --git a/js/src/playwright.ts b/js/src/playwright.ts index 55bf14e..c6a1c7d 100644 --- a/js/src/playwright.ts +++ b/js/src/playwright.ts @@ -44,6 +44,26 @@ function filterStealthCtxOptions(ctx?: BrowserContextOptions): Partial { } }); - it("exports buildLaunchOptions and humanizeBrowser from the package entrypoint", async () => { + it("exports composable helpers from the package entrypoint", async () => { const entry = await import("../src/index.js"); expect(entry.buildLaunchOptions).toBeTypeOf("function"); + expect(entry.buildContextOptions).toBeTypeOf("function"); expect(entry.humanizeBrowser).toBeTypeOf("function"); }); + it("buildContextOptions returns Playwright context options without launching a browser", async () => { + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); + const { buildContextOptions } = await import("../src/index.js"); + + const options = buildContextOptions({ + userAgent: "Explicit/1.0", + viewport: { width: 1280, height: 720 }, + colorScheme: "dark", + contextOptions: { + userAgent: "Context/9.9", + viewport: { width: 9999, height: 9999 }, + colorScheme: "light", + storageState: "state.json", + locale: "de-DE", + timezoneId: "Europe/Berlin", + }, + }); + + expect(options).toMatchObject({ + userAgent: "Explicit/1.0", + viewport: { width: 1280, height: 720 }, + colorScheme: "dark", + storageState: "state.json", + }); + expect(options.locale).toBeUndefined(); + expect(options.timezoneId).toBeUndefined(); + expect(warnSpy).toHaveBeenCalledTimes(2); + }); + + it("buildContextOptions applies DEFAULT_VIEWPORT by default and allows null viewport", async () => { + const { buildContextOptions } = await import("../src/index.js"); + + expect(buildContextOptions().viewport).toEqual(DEFAULT_VIEWPORT); + expect(buildContextOptions({ viewport: null }).viewport).toBeNull(); + }); + it("buildLaunchOptions returns Playwright options without launching a browser", async () => { const freshConfig = await import("../src/config.js"); vi.spyOn(freshConfig, "getPlatformTag").mockReturnValue("darwin-arm64");