From 4539c3f71be18ee7bfc1121dd0e48f99d4a31541 Mon Sep 17 00:00:00 2001 From: gmegidish Date: Sat, 9 May 2026 15:26:53 +0200 Subject: [PATCH] fix: honor --config flag in fixtures and device-pool setup --- packages/mobilewright/src/config.ts | 33 +++++++++++++------ .../mobilewright/src/device-pool/setup.ts | 2 +- packages/test/src/fixtures.ts | 8 ++--- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/packages/mobilewright/src/config.ts b/packages/mobilewright/src/config.ts index 7a90d2b..7c00ced 100644 --- a/packages/mobilewright/src/config.ts +++ b/packages/mobilewright/src/config.ts @@ -1,5 +1,5 @@ import { access } from 'node:fs/promises'; -import { join } from 'node:path'; +import { isAbsolute, join, resolve } from 'node:path'; import { pathToFileURL } from 'node:url'; import { createRequire } from 'node:module'; @@ -137,24 +137,37 @@ const CONFIG_FILES = [ 'mobilewright.config.mjs', ]; +async function importConfig(fullPath: string): Promise { + const mod = await import(pathToFileURL(fullPath).href); + let config = mod.default ?? mod; + // Some loaders (e.g. Playwright's TS transpiler) double-wrap the default export + if (config && typeof config === 'object' && 'default' in config) { + config = config.default; + } + return config as MobilewrightConfig; +} + /** - * Load mobilewright config from the project root. - * Returns empty config if no config file found. + * Load mobilewright config. + * + * If `configFile` is provided, that file is loaded directly. Otherwise scans + * `cwd` for mobilewright.config.{ts,js,mjs}. Returns empty config when nothing + * is found. */ export async function loadConfig( cwd: string = process.cwd(), + configFile?: string, ): Promise { + if (configFile) { + const fullPath = isAbsolute(configFile) ? configFile : resolve(cwd, configFile); + return importConfig(fullPath); + } + for (const name of CONFIG_FILES) { const fullPath = join(cwd, name); try { await access(fullPath); - const mod = await import(pathToFileURL(fullPath).href); - let config = mod.default ?? mod; - // Some loaders (e.g. Playwright's TS transpiler) double-wrap the default export - if (config && typeof config === 'object' && 'default' in config) { - config = config.default; - } - return config as MobilewrightConfig; + return importConfig(fullPath); } catch { continue; } diff --git a/packages/mobilewright/src/device-pool/setup.ts b/packages/mobilewright/src/device-pool/setup.ts index 03cbcca..a724ca7 100644 --- a/packages/mobilewright/src/device-pool/setup.ts +++ b/packages/mobilewright/src/device-pool/setup.ts @@ -18,7 +18,7 @@ let active: ActiveCoordinator | undefined; * that CLI overrides (e.g. --workers 2) are reflected in maxSlots. */ export default async function setup(playwrightConfig: FullConfig): Promise<() => Promise> { - const config = await loadConfig(); + const config = await loadConfig(process.cwd(), playwrightConfig.configFile); const { allocator, serverProcess } = await createAllocator(config); // Use the resolved worker count from Playwright's FullConfig so CLI flags diff --git a/packages/test/src/fixtures.ts b/packages/test/src/fixtures.ts index 79dc58f..56ded3e 100644 --- a/packages/test/src/fixtures.ts +++ b/packages/test/src/fixtures.ts @@ -45,16 +45,16 @@ function getClient(): DevicePoolClient { } export const test = base.extend({ - bundleId: [async ({}, use) => { - const config = await loadConfig(); + bundleId: [async ({}, use, testInfo) => { + const config = await loadConfig(process.cwd(), testInfo.config.configFile); await use(config.bundleId); }, { option: true }], platform: [undefined, { option: true }], deviceName: [undefined, { option: true }], - device: async ({ platform, deviceName, bundleId }, use) => { - const config = await loadConfig(); + device: async ({ platform, deviceName, bundleId }, use, testInfo) => { + const config = await loadConfig(process.cwd(), testInfo.config.configFile); const merged = { ...config, ...(platform && { platform }),