widget tests failing after updating to 5.9.0
mleevi opened this issue ยท 13 comments
Widget tests that contain ScreenUtilInit
fail when using 5.9.0
. Downgrading to 5.8.4
fixes the issue. Putting a tester.pumpAndSettle()
after every tester.pumpWidget
also solves the problem.
What kind of errors?
What kind of errors?
Finder does not find anything on the first pump (zero widgets found). After adding pumpAndsSettle after the initial pump fixes the errors.
Put here a minimal code to reproduce the error.
Passes with 5.8.4, fails with 5.9.0:
class Test extends StatefulWidget {
const Test({super.key});
@override
State<Test> createState() => _TestState();
}
class _TestState extends State<Test> {
@override
Widget build(BuildContext context) {
return ScreenUtilInit(
designSize: const Size(430, 932),
minTextAdapt: true,
splitScreenMode: true,
useInheritedMediaQuery: true,
child: const Scaffold(body: Text('text')),
builder: (_, child) => MaterialApp(
localizationsDelegates: const [
GlobalCupertinoLocalizations.delegate,
DefaultCupertinoLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
home: child,
),
);
}
}
void main() {
testWidgets('description', (tester) async {
await tester.pumpWidget(const Test());
expect(find.text('text'), findsOneWidget);
});
}
Well, that's unexpected. We're using Future in last version for the new change: ensureScreenSize as parameter. Thanks for reporting.
Is there any update on this? @Mounir-Bouaiche @techouse @lizhuoyuan @naivetoby
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.
Hello everyone, how did this work out in the end? I also encountered the same problem.
The same issue:
[โ] Flutter (Channel stable, 3.16.9, on macOS 14.2.1 23C71 darwin-arm64, locale es-ES)
โข Flutter version 3.16.9 on channel stable at /Users/gus/SDK/flutter
โข Upstream repository https://github.com/flutter/flutter.git
โข Framework revision 41456452f2 (13 days ago), 2024-01-25 10:06:23 -0800
โข Engine revision f40e976bed
โข Dart version 3.2.6
โข DevTools version 2.28.5
[โ] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
โข Android SDK at /Users/gus/Library/Android/sdk
โข Platform android-34, build-tools 34.0.0
โข Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
โข Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
โข All Android licenses accepted.
[โ] Xcode - develop for iOS and macOS (Xcode 15.0.1)
โข Xcode at /Applications/Xcode.app/Contents/Developer
โข Build 15A507
โข CocoaPods version 1.14.3
[โ] Chrome - develop for the web
โข Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[โ] Android Studio (version 2023.1)
โข Android Studio at /Applications/Android Studio.app/Contents
โข Flutter plugin can be installed from:
๐จ https://plugins.jetbrains.com/plugin/9212-flutter
โข Dart plugin can be installed from:
๐จ https://plugins.jetbrains.com/plugin/6351-dart
โข Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
[โ] VS Code (version 1.86.0)
โข VS Code at /Applications/Visual Studio Code.app/Contents
โข Flutter extension version 3.82.0
[โ] Network resources
โข All expected network resources are available.
With the test:
void main() {
testWidgets('should sign screen widgets exist', (WidgetTester tester) async {
await tester.pumpWidget(
const CupertinoApp(
home: SignPortraitScreen(),
),
);
final scaffoldFinder = find.byKey(const Key('scaffold_sign_portrait'));
expect(scaffoldFinder, findsOneWidget);
final navTitleFinder = find.byKey(const Key('sign_nav_title'));
expect(navTitleFinder, findsOneWidget);
});
}
The test passes when SignPortraitScreen() does not use any screen utilities, with a value like height: 100
, and fails when it has any value such as height: 100.h
.
Error:
โโโก EXCEPTION CAUGHT BY WIDGETS LIBRARY โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The following LateError was thrown building SignPortraitScreen(dirty):
LateInitializationError: Field '_splitScreenMode@289084504' has not been initialized.
The relevant error-causing widget was:
SignPortraitScreen
SignPortraitScreen:file:///Users/gus/Codigo/FlutterProjects/project/test/widgets/sign_screen_portrait_test.dart:11:15
When the exception was thrown, this was the stack:
#0 ScreenUtil._splitScreenMode (package:flutter_screenutil/src/screen_util.dart)
#1 ScreenUtil.scaleHeight (package:flutter_screenutil/src/screen_util.dart:204:8)
#2 ScreenUtil.setHeight (package:flutter_screenutil/src/screen_util.dart:225:44)
#3 SizeExtension.h (package:flutter_screenutil/src/size_extension.dart:12:32)
#4 SignPortraitScreen.build (package:story_creator/ui/screens/login/sign_portrait_screen.dart:33:31)
#5 StatelessElement.build (package:flutter/src/widgets/framework.dart:5541:49)
#6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5471:15)
Always fails with:
void main() {
testWidgets('should sign screen widgets exist', (WidgetTester tester) async {
await tester.pumpWidget(
const CupertinoApp(
home: ScreenUtilInit(
child: SignPortraitScreen(),
),
),
);
final scaffoldFinder = find.byKey(const Key('scaffold_sign_portrait'));
expect(scaffoldFinder, findsOneWidget);
final navTitleFinder = find.byKey(const Key('sign_nav_title'));
expect(navTitleFinder, findsOneWidget);
});
}
Then:
โโโก EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The following TestFailure was thrown running a test:
Expected: exactly one matching candidate
Actual: _KeyWidgetFinder:<Found 0 widgets with key [<'scaffold_sign_portrait'>]: []>
Which: means none were found but one was expected
When the exception was thrown, this was the stack:
#4 main.<anonymous closure> (file:///Users/gus/Codigo/FlutterProjects/project/test/widgets/sign_screen_portrait_test.dart:16:5)
<asynchronous suspension>
#5 testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:168:15)
<asynchronous suspension>
#6 TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1013:5)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)
This was caught by the test expectation on the following line:
file:///Users/gus/Codigo/FlutterProjects/project/test/widgets/sign_screen_portrait_test.dart line 16
The test description was:
should sign screen widgets exist
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Works fine with tester.pumpAndSettle()
:
testWidgets('should sign screen widgets exist', (WidgetTester tester) async {
await tester.pumpWidget(
const CupertinoApp(
home: ScreenUtilInit(
child: SignPortraitScreen(),
),
),
);
await tester.pumpAndSettle();
final scaffoldFinder = find.byKey(const Key('scaffold_sign_portrait'));
expect(scaffoldFinder, findsOneWidget);
final navTitleFinder = find.byKey(const Key('sign_nav_title'));
expect(navTitleFinder, findsOneWidget);
});
@Mounir-Bouaiche Is it possible to make a release or is this package now unmaintained?