Tests don't work in Flutter Web application with Firebase plugin

405

By default, pub run test (or clicking the "Run Tests" button on your IDE) runs tests on the Dart VM. But because of the dart:html import in the Firebase package, you need to run the tests on a browser, not Dart VM.

For example:

pub run test -p chrome path/to/test.dart

If all of your tests are in the /test folder, you can just say pub run test -p chrome.

Read more here: https://pub.dev/packages/test#running-tests

Share:
405
Dharman
Author by

Dharman

Stack Overflow elected moderator since 28th March 2022. Warning: You are wide open to SQL Injections and should use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input! Even when your queries are executed only by trusted users, you are still in risk of corrupting your data. Escaping is not enough!

Updated on December 14, 2022

Comments

  • Dharman
    Dharman over 1 year

    I created Flutter Web application. I am using Firebase plugin. The application works in browser, but when I am trying to run tests I am getting compilation error related to Firebase plugin.

    When I commented out the code related to Firebase plugin then a test is passing.

    This is my pubspec.yaml:

    environment:
      sdk: ">=2.1.0 <3.0.0"
    
    dependencies:
      flutter:
        sdk: flutter
      firebase: 5.0.4
      flutter_bloc: ^0.21.0
      meta: ^1.1.6
    
    dev_dependencies:
      flutter_test:
        sdk: flutter
    
    

    When I run tests I am getting:

    Compiler message:
    ../../../AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/firebase-5.0.4/lib/src/utils.dart:2:8: Error: Not found: 'dart:html'
    import 'dart:html' show promiseToFuture;
    

    So basically when running tests I don't have dart:html package imported. However when I start app normally everything works fine.

    Can you help me with importing this package for tests?

  • dazza5000
    dazza5000 over 4 years
    The issue is with the dart sdk and the firebase plugin. github.com/flutter/flutter/issues/42408
  • Admin
    Admin over 4 years
    In the meantime I upgraded flutter (I'm now on Flutter 1.10.15-pre.218 - channel master). And now when I am running tests using "Run Tests" button then they're passing. But when I am using the command that you suggested then I'm getting compilation error: Failed to load "path/to/test.dart": dart2js failed.
  • Admin
    Admin over 4 years
    I found out that when I run widget tests (with testWidgets function) using 'Run Tests' button, then they're passing. But when I run unit tests (with test function) then I have the same error that I mentioned in the question.
  • skillit zimberg
    skillit zimberg about 4 years
    @IiroKrankka your answer worked for me. Thank you! Is this the dart:html import you're talking about: firebase-dart/lib/src/top_level.dart? And how did you know it was there? I had to dig deep to find it and only because you told me it was there.