Widget tests starts to fail when using PdfViewer widget
Closed this issue · 3 comments
Leonardo-Caracho commented
Hi,
First I would like to thank you for this amazing package, my problem with it is that my widget tests started to fail once I started to using it, replacing pdf_render. The problem seems to be related with a timer not being disposable correctly inside the package, maybe is related to the #246. The code is quite simple, I pasted below as long with the error message.
I tried with both PdfViewer.data
and PdfViewer.file
I got the same error for both.
class TicketPdfPreview extends StatelessWidget {
const TicketPdfPreview({
super.key,
required this.data,
});
final Uint8List data;
@override
Widget build(BuildContext context) {
return PdfViewer.data(
data,
sourceName: '',
params: PdfViewerParams(
backgroundColor: Colors.transparent,
errorBannerBuilder: (context, error, stackTrace, documentRef) {
return ErrorWidget(error);
},
),
);
;
}
}
Widget test
testWidgets('TicketPreview renders.', (tester) async {
await tester.pumpWidget(
widgetFramework(
child: TicketPreview(
ticket: 'html',
width: 3,
printerUtils: ts.buildPrinterUtils(),
),
),
);
await tester.pumpAndSettle();
expect(find.byType(TicketPdfPreview), findsOneWidget);
expect(find.byType(PdfViewer), findsOneWidget);
});
Exceptions
════════════════════════════════════════════════════════════════════════════════════════════════════
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
A Timer is still pending even after the widget tree was disposed.
'package:flutter_test/src/binding.dart':
Failed assertion: line 1542 pos 12: '!timersPending'
When the exception was thrown, this was the stack:
#2 AutomatedTestWidgetsFlutterBinding._verifyInvariants (package:flutter_test/src/binding.dart:1542:12)
#3 TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1049:7)
<asynchronous suspension>
<asynchronous suspension>
(elided 3 frames from class _AssertionError and package:stack_trace)
The test description was:
TicketPreview renders.
════════════════════════════════════════════════════════════════════════════════════════════════════
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following message was thrown:
Multiple exceptions (2) were detected during the running of the current test, and at least one was
unexpected.
════════════════════════════════════════════════════════════════════════════════════════════════════
espresso3389 commented
Although I'm not sure about the testability of PdfViewer widget (it's completely async) but at least 4ae2b02 addresses the timer disposal issue.
espresso3389 commented
1.0.93 contains the fix.
Leonardo-Caracho commented
1.0.93 contains the fix.
thanks, now the tests are passing.