Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix
  • Loading branch information
dkwingsmt committed Sep 28, 2026
commit c617498f89a821dd1e4e2f792544886ab224fde6
59 changes: 53 additions & 6 deletions packages/flutter_test/lib/src/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
);
}

_currentTestDescription = description;
_oldExceptionHandler = FlutterError.onError;
_oldStackTraceDemangler = FlutterError.demangleStackTrace;
var exceptionCount = 0; // number of un-taken exceptions
Expand Down Expand Up @@ -1989,6 +1990,9 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
asyncBarrier(); // When using AutomatedTestWidgetsFlutterBinding, this flushes the microtasks.
}

// The description of the currently running test, saved in [_runTest] so that
// [postTest] can include it when reporting invariant failures.
String _currentTestDescription = '';
bool _beforeTestAutoUpdateGoldens = false;
late TestExceptionReporter _beforeTestReportTestException;
late ErrorWidgetBuilder _beforeTestErrorWidgetBuilder;
Expand Down Expand Up @@ -2087,7 +2091,13 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
void _verifyAutoUpdateGoldensUnset(bool valueBeforeTest) {
assert(() {
if (autoUpdateGoldenFiles != valueBeforeTest) {
throw FlutterError('The value of autoUpdateGoldenFiles was changed by the test.');
FlutterError.reportError(
FlutterErrorDetails(
exception: FlutterError('The value of autoUpdateGoldenFiles was changed by the test.'),
stack: StackTrace.current,
library: 'Flutter test framework',
),
);
}
return true;
}());
Expand All @@ -2101,7 +2111,13 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
// So we reset the error reporter to its initial value and then report
// this error.
reportTestException = valueBeforeTest;
throw FlutterError('The value of reportTestException was changed by the test.');
FlutterError.reportError(
FlutterErrorDetails(
exception: FlutterError('The value of reportTestException was changed by the test.'),
stack: StackTrace.current,
library: 'Flutter test framework',
),
);
}
return true;
}());
Expand All @@ -2110,7 +2126,13 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
void _verifyErrorWidgetBuilderUnset(ErrorWidgetBuilder valueBeforeTest) {
assert(() {
if (ErrorWidget.builder != valueBeforeTest) {
throw FlutterError('The value of ErrorWidget.builder was changed by the test.');
FlutterError.reportError(
FlutterErrorDetails(
exception: FlutterError('The value of ErrorWidget.builder was changed by the test.'),
stack: StackTrace.current,
library: 'Flutter test framework',
),
);
}
return true;
}());
Expand All @@ -2119,8 +2141,14 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
void _verifyShouldPropagateDevicePointerEventsUnset(bool valueBeforeTest) {
assert(() {
if (shouldPropagateDevicePointerEvents != valueBeforeTest) {
throw FlutterError(
'The value of shouldPropagateDevicePointerEvents was changed by the test.',
FlutterError.reportError(
FlutterErrorDetails(
exception: FlutterError(
'The value of shouldPropagateDevicePointerEvents was changed by the test.',
),
stack: StackTrace.current,
library: 'Flutter test framework',
),
);
}
return true;
Expand Down Expand Up @@ -2185,7 +2213,25 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
library: 'Flutter test framework',
);
}
// Some invariant checks (such as debugAssertNoTransientCallbacks and the
// _verify*Unset helpers above) report failures through
// FlutterError.reportError instead of throwing. During the test,
// FlutterError.onError stores them in _pendingExceptionDetails, which is
// normally reported by the test completion handler. That handler has
// already run by the time postTest is called, so these errors must be
// picked up here or they would be silently dropped.
final FlutterErrorDetails? reportedError = _pendingExceptionDetails;
if (reportedError != null) {
if (invariantError == null) {
invariantError = reportedError;
} else {
debugPrint = debugPrintOverride; // just in case the test overrides it -- otherwise we won't see the error!
FlutterError.dumpErrorToConsole(reportedError, forceReport: true);
}
}
}
final String testDescription = _currentTestDescription;
_currentTestDescription = '';
FlutterError.onError = _oldExceptionHandler;
FlutterError.demangleStackTrace = _oldStackTraceDemangler;
_pendingExceptionDetails = null;
Expand Down Expand Up @@ -2223,7 +2269,8 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
ServicesBinding.instance.resetInternalState();

if (invariantError != null) {
reportTestException(invariantError, '');
debugPrint = debugPrintOverride; // just in case the test overrides it -- otherwise we won't see the error!
reportTestException(invariantError, testDescription);
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions packages/flutter_test/test/bindings_invariants_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

Expand Down Expand Up @@ -171,4 +172,45 @@ void main() {
});
},
);

// Verifies that invariant checks that report failures via
// [FlutterError.reportError] instead of throwing (such as
// [SchedulerBinding.debugAssertNoTransientCallbacks]) are captured and
// reported with the test description in [TestWidgetsFlutterBinding.postTest].
test('postTest reports invariant failures logged via FlutterError.reportError', () async {
FlutterErrorDetails? reportedError;
String? reportedDescription;
final TestExceptionReporter oldReporter = reportTestException;
reportTestException = (FlutterErrorDetails details, String testDescription) {
reportedError = details;
reportedDescription = testDescription;
};
addTearDown(() {
reportTestException = oldReporter;
});

final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
// A Ticker reschedules its frame callback on every tick, so it survives the
// frames pumped while unmounting the tree. A one-shot frame callback would
// be consumed by those frames instead.
late final Ticker ticker;
await binding.runTest(
() async {
ticker = Ticker((Duration _) {})..start();
},
() {},
description: 'leaking ticker test',
);

expect(() => binding.postTest(), returnsNormally);
expect(reportedError, isNotNull);
expect(
reportedError!.exception,
'An animation is still running even after the widget tree was disposed.',
);
expect(reportedDescription, 'leaking ticker test');
expect(binding.inTest, isFalse);

ticker.dispose();
});
Comment thread
dkwingsmt marked this conversation as resolved.
Resolved
}
Loading