fix(browser-runner): hoist mocks before side-effect imports by carrerasdarren-cell · Pull Request #16112 · webdriverio/webdriverio · GitHub
Skip to content

fix(browser-runner): hoist mocks before side-effect imports - #16112

Open
carrerasdarren-cell wants to merge 1 commit into
webdriverio:mainfrom
carrerasdarren-cell:codex/side-effect-mock-hoisting-12624-20261017
Open

carrerasdarren-cell wants to merge 1 commit into
webdriverio:mainfrom
carrerasdarren-cell:codex/side-effect-mock-hoisting-12624-20261017

Conversation

@carrerasdarren-cell

Copy link
Copy Markdown
Contributor

Proposed changes

Fixes #12624. A side-effect-only import in a factory-mocked component spec stays static today, so its module evaluates before the hoisted mock factory. A custom element can therefore capture the real dependency even though the equivalent binding import sees the mock.

Rewrite bare imports in the active spec as sequential await import(...) expressions at their original positions. Dependency-level bare imports remain static, and specs without factory mocks retain their original code. The existing actual-module fallback is preserved. Add transform regressions and six separate component specs, wired into the normal browser-test chain.

How you tested

  • Before the fix, a local real-Chrome component reproduction failed with the real evaluation-time marker; the binding-import and no-mock controls passed. The exact same three assertion fixtures passed after compiling the fix.
  • pnpm run test:package wdio-browser-runner: 115 tests across 15 files passed, including explicit transform ordering, no-factory/manual/unmock controls and valid actual-namespace fallback.
  • pnpm --dir e2e run test:browser:module-mocks: all six final component specs passed in Chrome 158 with standard coverage enabled (100% statements, branches, functions and lines). They cover an asynchronous factory, mixed bare/binding import order, single evaluation after a duplicate import, directly mocked bare imports with and without an actual-module argument, and real/no-mock behavior.
  • pnpm exec tsc --noEmit -p packages/wdio-browser-runner/tsconfig.json, pnpm run test:oxlint, and focused oxlint for all 13 added component/spec JavaScript files passed.
  • Component baseline limitation: the full pnpm run test:component command exits 1 at two existing Vue multi-element matcher assertions. Continuing the skipped standard lanes passed Svelte, Lit and Stencil; misc passed its four browser-error tests and five network-mock tests but failed two existing image-size assertions. The uncaught-error checker passed its expected two passing/one failing scenario. Both failure families reproduced with the original 3b5e5df transform after recompiling in the same configuration/dependency/Chrome environment: the same two Vue failures and the same two image dimensions (256×213 and 256×256). The reviewed fix and deterministic compiled bundle were then restored byte-for-byte. The full component suite is not claimed to pass.

All local component runs used Chrome with CI unset. The permanent specs retain browser-independent marker assertions.

  • Unit tests for the touched package(s)
  • Type definition tests (public API unchanged; source type-check passed)
  • Smoke or e2e (real component specs)

Types of changes

  • Polish
  • Bugfix
  • New feature
  • Breaking change
  • Documentation update
  • Specification changes
  • Internal updates

Checklist

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective
  • I have added necessary documentation (no public API or configuration change)
  • I have added proper type definitions for new commands (no new commands)

Backport Request

  • This change is solely for v10 and does not need to be back-ported
  • Back-ported PR at #XXXXX

Further comments

Implemented and tested with OpenAI Codex assistance. No v9 backport was evaluated. This change preserves actual-module evaluation for factories that request it, rather than introducing a new promise to suppress real-module side effects.

@carrerasdarren-cell
carrerasdarren-cell requested a review from a team as a code owner October 17, 2026 04:40
@wdio-bot wdio-bot added the PR: Bug Fix 🐛 PRs that contain bug fixes label Oct 17, 2026
@greptile-apps

greptile-apps Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Bug Fix 🐛 PRs that contain bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[🐛 Bug]: Mock hoisting fails for direct imports

2 participants