runTest method Null safety

  1. @override
Future<void> runTest(
  1. Future<void> testBody(
    1. VoidCallback invariantTester,
    2. {String description = '',
    3. Duration? timeout}

    Call the testBody inside a FakeAsync scope on which pump can advance time.

    Returns a future which completes when the test has run.

    Called by the testWidgets and benchmarkWidgets functions to run a test.

    The invariantTester argument is called after the testBody's Future completes. If it throws, then the test is marked as failed.

    The description is used by the LiveTestWidgetsFlutterBinding to show a label on the screen during the test. The description comes from the value passed to testWidgets. It must not be null.

    The timeout argument sets the initial timeout, if any. It can be increased with addTime. By default there is no timeout.


    Future<void> runTest(
      Future<void> Function() testBody,
      VoidCallback invariantTester, {
      String description = '',
      Duration? timeout,
    }) {
      assert(description != null);
      assert(_currentFakeAsync == null);
      assert(_clock == null);
      _timeout = timeout;
      if (_timeout != null) {
        _timeoutStopwatch = Stopwatch()..start();
        _timeoutTimer = Timer.periodic(const Duration(seconds: 1), _checkTimeout);
        _timeoutCompleter = Completer<void>();
      final FakeAsync fakeAsync = FakeAsync();
      _currentFakeAsync = fakeAsync; // reset in postTest
      _clock = fakeAsync.getClock(DateTime.utc(2015, 1, 1));
      late Future<void> testBodyResult; localFakeAsync) {
        assert(fakeAsync == _currentFakeAsync);
        assert(fakeAsync == localFakeAsync);
        testBodyResult = _runTest(testBody, invariantTester, description, timeout: _timeoutCompleter?.future);
      return Future<void>.microtask(() async {
        // testBodyResult is a Future that was created in the Zone of the
        // fakeAsync. This means that if we await it here, it will register a
        // microtask to handle the future _in the fake async zone_. We avoid this
        // by calling '.then' in the current zone. While flushing the microtasks
        // of the fake-zone below, the new future will be completed and can then
        // be used without fakeAsync.
        final Future<void> resultFuture = testBodyResult.then<void>((_) {
          // Do nothing.
        // Resolve interplay between fake async and real async calls.
        while (_pendingAsyncTasks != null) {
          await _pendingAsyncTasks!.future;
        return resultFuture;