Garrett Barlocker
Garrett Barlocker

Reputation: 703

Flutter integration test running twice on web

GitHub issue

Setup:

  1. Clone repository
  2. Run chromedriver --port=4444 in another terminal
  3. Run flutter drive --driver=test_driver/integration_test.dart --target=integration_test/example_test.dart -d chrome

It seems like the test is running twice as there are two documents inserted into Firestore where there should only be one document inserted.

This behavior is only happening on web, it doesn't produce the same behavior on iOS

If you clone the repo and test it for yourself please change the collection name of examples to something else to not clash with other testers

Code for when the repository is removed

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  final now = DateTime.now();
  const String collectionName = 'examples';
  String currentMinute = DateFormat.yMd().add_jm().format(now);

  documentsInsertedThisMinute() async {
    var collection = FirebaseFirestore.instance.collection(collectionName);
    var snapshots =
        await collection.where('foobar', isEqualTo: currentMinute).get();
    return snapshots.size;
  }

  setUpAll(() async {
    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );
  });

  testWidgets(
    'Single document created',
    (WidgetTester tester) async {
      int preExistingDocuments = await documentsInsertedThisMinute();
      print('Pre-existing documents: $preExistingDocuments');

      await tester.pumpWidget(const MyApp());

      await FirebaseFirestore.instance
          .collection(collectionName)
          .doc()
          .set({'foobar': currentMinute});

      int documentsAfterSingleInsert = await documentsInsertedThisMinute();
      expect(documentsAfterSingleInsert, 1);
    },
  );
}

Upvotes: 3

Views: 979

Answers (2)

Divyansh Ingle
Divyansh Ingle

Reputation: 370

Using the below command, I successfully launched a single Chrome instance which has the label chrome is controlled by automatically

flutter drive --driver="test_driver/integration_test.dart" --target="integration_test/integration_test.dart" -d web-server --browser-name chrome --no-headless

Note: I tested on flutter version 3.7.0

Upvotes: 2

Xao
Xao

Reputation: 591

I had the same issues with running the code twice when using -d chrome with or without --headless flag.

What I use:

Multiline Linux

flutter drive \
--driver=test_driver/integration_test.dart \
--target=integration_test/sec_rules_test.dart \
--device-id web-server \
--dart-define=PROJECT_ID=someProjectId

Multiline Windows (Powershell)

flutter drive `
--driver=test_driver/integration_test.dart `
--target=integration_test/example_test.dart `
--device-id web-server `
--dart-define=PROJECT_ID=someProjectId

Also documented here, of course it doesn't specify if you run it in -d chrome it will behave as such.

Upvotes: 2

Related Questions