Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't .toJS a Future<Never> function. #56369

Open
ditman opened this issue Aug 3, 2024 · 3 comments
Open

Can't .toJS a Future<Never> function. #56369

ditman opened this issue Aug 3, 2024 · 3 comments
Assignees
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-question A question about expected behavior or functionality web-js-interop Issues that impact all js interop

Comments

@ditman
Copy link
Member

ditman commented Aug 3, 2024

We've been porting some old test code to a newer style, and I was trying to "mock" a function that ends up throwing:

mockMediaDevices.getUserMedia = ([web.MediaStreamConstraints? _]) {
  throw web.DOMException('', 'NotFoundError');
}.toJS;

However the compiler complains that it doesn't like Never:

org-dartlang-app:/camera_service_test.dart:103:13: Error: Function converted via 'toJS' contains invalid types in its
function signature: '*Never* Function(MediaStreamConstraints?)'.
Use one of these valid types instead: JS types from 'dart:js_interop', ExternalDartReference, void, bool, num,
double, int, String, extension types that erase to one of these types, '@staticInterop' types, 'dart:html' types when
compiling to JS, or a type parameter that is a subtype of a valid non-primitive type.
          }.toJS;
            ^
the Dart compiler exited unexpectedly.
Failed to compile application.

So I need to end up doing:

mockMediaDevices.getUserMedia = ([web.MediaStreamConstraints? _]) {
  throw web.DOMException('', 'NotFoundError');
  // ignore: dead_code
  return Future<web.MediaStream>.value(web.MediaStream()).toJS;
}.toJS;

Is there a more graceful way to handle something like this?

@dart-github-bot
Copy link
Collaborator

Summary: The toJS method cannot be used on a function that returns Future<Never> because it throws an exception and never completes. The user is looking for a more elegant way to mock a function that throws an exception.

@dart-github-bot dart-github-bot added area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-question A question about expected behavior or functionality labels Aug 3, 2024
@ditman
Copy link
Member Author

ditman commented Aug 3, 2024

Good bot @dart-github-bot !

@lrhn lrhn added web-js-interop Issues that impact all js interop and removed triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Aug 3, 2024
@srujzs srujzs self-assigned this Aug 5, 2024
@srujzs
Copy link
Contributor

srujzs commented Aug 5, 2024

I think the conflict is really Never and not Future<Never> looking at the static type.

At some point we discussed supporting Never in the allowed types but decided against it for some reason I don't remember. Maybe because the exception is not really a JS value? That's an insufficient reason if so, and we should support this.

A cleaner workaround may be to cast the function to a valid type e.g.

((() => throw 'done') as void Function()).toJS;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-question A question about expected behavior or functionality web-js-interop Issues that impact all js interop
Projects
Status: No status
Development

No branches or pull requests

4 participants