Yarn berry compat

This commit is contained in:
Quine Global 2025-09-07 15:28:22 -07:00 committed by Philip Peterson
parent 4ca4677177
commit b1e120cc49
2 changed files with 29 additions and 2 deletions

View file

@ -1,5 +1,5 @@
import exitHook from 'async-exit-hook'
import { resolve } from 'import-meta-resolve'
import { createRequire } from 'node:module'
import logger from '@wdio/logger'
import { validateConfig } from '@wdio/config'
@ -13,6 +13,7 @@ import { runLauncherHook, runOnCompleteHook, runServiceHook, nodeVersion, type H
import { TESTRUNNER_DEFAULTS, WORKER_GROUPLOGS_MESSAGES } from './constants.js'
import type { RunCommandArguments } from './types.js'
const log = logger('@wdio/cli:launcher')
const require = createRequire(import.meta.url)
interface Schedule {
cid: number
@ -179,7 +180,7 @@ class Launcher {
/**
* add tsx to process NODE_OPTIONS so it will be passed along the worker process
*/
const tsxPath = resolve('tsx', import.meta.url)
const tsxPath = require.resolve('tsx')
if (!process.env.NODE_OPTIONS || !process.env.NODE_OPTIONS.includes(tsxPath)) {
/**
* The `--import` flag is only available in Node 20.6.0 / 18.19.0 and later.

View file

@ -244,6 +244,32 @@ export async function safeImport (name: string): Promise<Services.ServicePlugin
* Only in Node.js environments
*/
if (!globalThis.window) {
// For Yarn Berry compatibility, try direct import first
try {
const pkg = await import(/* @vite-ignore */name)
/**
* CJS packages build with TS imported through an ESM context can end up being this:
*
* [Module: null prototype] {
* __esModule: true,
* default: {
* launcher: [class SmokeServiceLauncher],
* default: [class SmokeService]
* },
* launcher: [class SmokeServiceLauncher]
* }
*
* In order to not have the testrunner ignore importing a service we should double check if
* a nested default is given and return that.
*/
if (pkg.default && pkg.default.default) {
return pkg.default
}
return pkg
} catch {
// Fallback to import-meta-resolve approach
}
const { resolve } = await import('import-meta-resolve')
try {
importPath = await resolve(name, import.meta.url)