Skip to content

mockito: multiplex overloaded @JS exports in extension type mocks - #5250

Closed
kevmoo wants to merge 3 commits into
masterfrom
mockito-js-overloads
Closed

kevmoo wants to merge 3 commits into
masterfrom
mockito-js-overloads

Conversation

@kevmoo

@kevmoo kevmoo commented Oct 29, 2026

Copy link
Copy Markdown
Member

Generate @JSExport multiplexer methods and setters on Mock*Target so MockSpec supports JS interop extension types with multiple external members bound to the same JS property name, such as (TrustedHTML or String) DOM overloads in dart-lang/web#596.

  • Colliding concrete overrides on Mock*Target export under distinct $mockito$... keys while paired getters and setters still share one JS property. This includes unsupportedMembers without fallbackGenerators, preventing concrete UnsupportedError stubs from colliding with active overloads.
  • Generated $jsExport$set$<name> and $jsExport$method$<name> multiplexers dispatch by arity and JSAny?.isA<T>() / isA<T?>() checks. Primitive, SDK JS, and nominal @JS extension type arguments route to the matching Invocation stub, boxing mixed return types via .toJS.
  • Runtime-indistinguishable overloads fail at build time with InvalidMockitoAnnotationException and unsupportedMembers guidance. Colliding getters, method/accessor conflicts, int vs. double JS numbers, and anonymous @JS() object-literal extension types cannot be distinguished by instanceof or typeof in JavaScript.

Generate `@JSExport` multiplexer methods and setters on `Mock*Target`
when a mocked JS interop `extension type` exposes multiple external
members bound to the same JS property name (such as `(TrustedHTML or
String)` DOM overloads in `package:web`).

- Rename colliding concrete overrides on `Mock*Target` (including
  `unsupportedMembers` without `fallbackGenerators`) to distinct
  `$mockito$get$...`, `$mockito$set$...`, and `$mockito$method$...`
  export keys so `createJSInteropWrapper` passes CFE `ExportChecker`
  validation, while preserving paired strict getter + strict setter
  exports under a shared JS property name.
- Dispatch incoming JS calls by arity and `JSAny?.isA<T>()` /
  `isA<T?>()` runtime checks across primitives, SDK JS types, and
  nominal `@JS` extension types, boxing mixed return types via `.toJS`.
- Throw `InvalidMockitoAnnotationException` at `build_runner` time when
  active `@JS` overloads are indistinguishable at runtime (such as
  colliding getters, method/accessor conflicts, `int` vs. `double` JS
  numbers, or anonymous object-literal extension types), guiding users
  to `MockSpec(unsupportedMembers: {#...})`.
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

PR Health

Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

This check can be disabled by tagging the PR with skip-changelog-check (for example, for non-user-visible changes when the package version is already at -wip).

Import `js_interop_overloads.dart` directly from `builder.dart` instead
of linking it via `part` / `part of`, passing explicit target metadata
and the library's `JsTypeReferenceResolver` callback into
`JsExportPlan`.
…nd expand tests

- Support non-JS extension types wrapping primitives on overloaded `@JS`
  parameters and mixed return types.
- Preserve `@JSExport` property names for `unsupportedMembers` entries when
  their slot in the JS export bucket is unoccupied, disambiguating only on
  collision.
- Respect per-candidate parameter nullability past `firstOptional` during
  optional overload dispatch synthesis.
- Unify representation-chain walking, parameter classification, conflict
  diagnostics, and cached `JsExportPlan` resolution on `_MockTarget`.
- Match allocator import prefixes dynamically in `custom_mocks_test.dart` and
  add unit and browser (`dart2js` / `dart2wasm`) coverage in
  `js_interop_overloads_test.dart` and `dart_js_interop_test.dart`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant