Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions packages/mobilewright/src/config.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -137,24 +137,37 @@ const CONFIG_FILES = [
'mobilewright.config.mjs',
];

async function importConfig(fullPath: string): Promise<MobilewrightConfig> {
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<MobilewrightConfig> {
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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/mobilewright/src/device-pool/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>> {
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
Expand Down
8 changes: 4 additions & 4 deletions packages/test/src/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ function getClient(): DevicePoolClient {
}

export const test = base.extend<MobilewrightTestFixtures>({
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 }),
Expand Down
Loading