Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deactivate or ignore layout overflow messages in Flutter widget tests?

Tests run with the Ahem font, which is too big, and sometimes it overflows, breaking tests. Some tests don't care about overflow anyway, so there should be a way to deactivate them.

I have many tests which run OK in the simulator, but break in tests.

We are forced to prevent overflows for widgets which will never overflow in reality, or else provide fonts for the tests, instead of Ahem, just for the sake of not overflowing the tests. It makes no sense that overflow errors are tested, unless you are doing "overflow error tests".

How do I turn off these errors, or how do I make tests ignore them?

like image 451
MarcG Avatar asked Aug 14 '19 17:08

MarcG


People also ask

How do I stop my widget overflow fluttering?

To make the Text widget expanded, select the widget, and from the Properties Panel choose the second icon beside Expansion. This would prevent the overflow issue and soft wrap the entire text.

How do you turn on overflow in flutter?

You can use a widget called OverflowBox . It allows you to impose different constraints to the child and makes it possible for the child to overflow its parent.

What is widget testing in flutter?

The widget test is testing UI components, so it is also called Component Testing. It is used to test a single widget. The main goal of widget testing is to check whether the widget works as expected. A user can write test cases for every widget present in the project.


2 Answers

Based on @RémiRousselet answer, I developed a solution.

FlutterError.onError = _onError_ignoreOverflowErrors;

Function _onError_ignoreOverflowErrors = (
  FlutterErrorDetails details, {
  bool forceReport = false,
}) {
  assert(details != null);
  assert(details.exception != null);
  // ---

  bool ifIsOverflowError = false;

  // Detect overflow error.
  var exception = details.exception;
  if (exception is FlutterError)
    ifIsOverflowError = !exception.diagnostics
        .any((e) => e.value.toString().startsWith("A RenderFlex overflowed by"));

  // Ignore if is overflow error.
  if (ifIsOverflowError)
    print('Overflow error.');

  // Throw others errors.
  else
    FlutterError.dumpErrorToConsole(details, forceReport: forceReport);
};

This function ignores just overflow exceptions.

like image 69
Eduardo Yamauchi Avatar answered Nov 02 '22 10:11

Eduardo Yamauchi


You cannot disable overflow specifically. But there are a few alternatives:

  • Change the font to a "real" font. Your tests don't have to use Ahem font. The documentation is lacking, here's the issue that added this feature: https://github.com/flutter/flutter/issues/17700
  • Change the virtual screen size. See How to test Flutter widgets on different screen sizes?
  • set FlutterError.onError to null
like image 37
Rémi Rousselet Avatar answered Nov 02 '22 11:11

Rémi Rousselet