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

🐛 [cloud_firestore] Need working example for Firestore Emulator in Widget tests - initializeApp hangs indefinitely #16727

Open
normidar opened this issue Nov 20, 2024 · 5 comments
Labels
blocked: customer-response Waiting for customer response, e.g. more information was requested. platform: all Issues / PRs which are for all platforms. plugin: core type: enhancement New feature or request

Comments

@normidar
Copy link

What feature would you like to see?

Issue Description

When trying to use the Firestore Emulator in Widget tests (testWidgets), Firebase.initializeApp() hangs indefinitely. We need a working example of how to properly set up and use the Firestore Emulator in Widget tests.

Current Code

testWidgets('test emulate', (tester) async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: const FirebaseOptions(
        apiKey: 'fake-api-key',
        appId: 'fake-app-id',
        messagingSenderId: 'fake-sender-id',
        projectId: 'demo-project',
      ),
    );
    FirebaseFirestore.instance.settings = const Settings(
      host: 'localhost:8085',
      sslEnabled: false,
      persistenceEnabled: false,
  );
});

Why This Matters

Fake Firestore don't support all Firestore features. Having the ability to test with the actual Firestore Emulator would allow us to:

  1. Test with real Firestore behavior
  2. Use features not available in fake implementations
  3. Have more confidence in our tests

What I'm Looking For

  1. A working example of how to properly set up the Firestore Emulator in Widget tests
  2. If this is not currently possible, guidance on what needs to be implemented to make it possible

Environment

  • firebase_core version: 3.4.0
  • cloud_firestore version: 5.5.0
  • Platform: Mac OS M1 chip
  • Flutter version:
Flutter 3.24.4 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 603104015d (4 weeks ago) • 2024-10-24 08:01:25 -0700
Engine • revision db49896cf2
Tools • Dart 3.5.4 • DevTools 2.37.3
@normidar normidar added Needs Attention This issue needs maintainer attention. type: enhancement New feature or request labels Nov 20, 2024
@SelaseKay SelaseKay added plugin: core platform: all Issues / PRs which are for all platforms. labels Nov 20, 2024
@SelaseKay
Copy link
Contributor

Hi @normidar, thanks for raising this concern. However, I don't think this is the right way to go about it. Can you try something like this to see if it resolves your issue:

void main() {
  setUpAll(() async {
    WidgetsFlutterBinding.ensureInitialized();
    
    await Firebase.initializeApp(
      options: const FirebaseOptions(
        apiKey: 'fake-api-key',
        appId: 'fake-app-id',
        messagingSenderId: 'fake-sender-id',
        projectId: 'demo-project',
      ),
    );
    
    FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8085);
  });


  testWidgets('some firestore test', (WidgetTester tester) async {
    // Test logic here, Firebase is already initialized
  });
}

@SelaseKay SelaseKay added the blocked: customer-response Waiting for customer response, e.g. more information was requested. label Nov 21, 2024
@normidar
Copy link
Author

Thank you for your response.

Is it okay to use dummy values like 'fake-api-key' in the test environment, or do I need to use real Firebase credentials?

@google-oss-bot google-oss-bot removed the blocked: customer-response Waiting for customer response, e.g. more information was requested. label Nov 22, 2024
@normidar
Copy link
Author

I ran that and got an error:

PlatformException(channel-error, Unable to establish connection on channel., null, null)
package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart 210:7  FirebaseCoreHostApi.initializeCore

@SelaseKay
Copy link
Contributor

You'll have to use your real Firebase credentials. Did you start your firebase emulator before running the test?

@SelaseKay SelaseKay added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Nov 22, 2024
@russellwheatley
Copy link
Member

@normidar - we have firestore test setup in the repository that you can follow. https://github.com/firebase/flutterfire/blob/main/packages/cloud_firestore/cloud_firestore/example/integration_test/e2e_test.dart

Pay attention to test_driver/ and integration_test directory setup.

Here is how to run: https://github.com/firebase/flutterfire/blob/main/.github/workflows/android.yaml#L90

As @SelaseKay mentioned - run firebase emualtors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked: customer-response Waiting for customer response, e.g. more information was requested. platform: all Issues / PRs which are for all platforms. plugin: core type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants