ReactiveX / rxdart

The Reactive Extensions for Dart

Home Page:http://reactivex.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot await stream after upgrading to rxdart 0.27.1 to 0.27.2 in Flutter tests

voidsp opened this issue · comments

Hello! After I have upgraded from rxdart 0.27.1 to 0.27.5, my tests have started failing. I found the test starts failing from rxdart 0.27.2:

import 'package:flutter_test/flutter_test.dart';
import 'package:rxdart/subjects.dart';

void main() {
  group('subject test', () {
    testWidgets('wait first', (WidgetTester tester) async {
      final subject = BehaviorSubject<String>();
      subject.add('my-value');

      final Stream<String> stream = subject.stream;

      expect(await stream.first, 'my-value');

      subject.close();
    });
  });
}

This test only fails when using flutter_test instead of test (however, in the actual test case I need to use flutter_test as there are some widgets being tested), so the interesting thing is this test will work:

import 'package:flutter_test/flutter_test.dart';
import 'package:rxdart/subjects.dart';

void main() {
  group('subject test', () {
    test('wait first', () async {
      final subject = BehaviorSubject<String>();
      subject.add('my-value');

      final Stream<String> stream = subject.stream;

      expect(await stream.first, 'my-value');

      subject.close();
    });
  });
}

The first code snippet test (with flutter_test) now just hangs, but previously it ran correctly in rxdart 0.27.1. The second code snippet (with test) works on all version of rxdart.

I wrap await stream.first in tester.runAsync and it works.

  group('subject test', () {
    testWidgets('wait first', (tester) async {
      final subject = BehaviorSubject<String>();
      subject.add('my-value');

      final Stream<String> stream = subject.stream;

      await tester.runAsync(() async {
        expect(await stream.first, 'my-value');
      });

      await subject.close();
    });
  });

Thank you @hoc081098, is this a regression in rxdart? I'm not sure why wrapping it in tester.runAsync is now required.

I'm still running into this - unfortunately wrapping our entire widget test in runAsync causes it to fail for other reasons so its not a reasonable workaround for us.

I've found that this for some reason fixes the tests await stream.doOnData((_) {}).first but seems like an ugly hack.