Fixing `Unable to load asset: FontManifest.json` when using golden_toolkit
I recently started using eBay's golden_toolkit for screenshot tests instead of Flutter's built-in matchesGoldenFile. My main issue with the built-in function was that it would use the Ahem font, which is all blocks. It makes it much harder to see if your app rendered correctly.
In the past, I had written about using the built-in function at http://blog.wafrat.com/screenshot-tests-in-flutter/.
eBay's golden_toolkit usage is similar. The main difference is you have to run the tests in a function called testGoldens
and run the test by calling screenMatchesGolden
:
import 'package:flutter_test/flutter_test.dart';
import 'package:golden_toolkit/golden_toolkit.dart';
import 'package:my_project/main.dart';
void main() {
testGoldens('Renders the app', (WidgetTester tester) async {
await loadAppFonts();
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
await screenMatchesGolden(tester, 'first_screen');
});
}
Upon running the test with golden_toolkit, I got this error:
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
Unable to load asset: FontManifest.json
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)
...
The test description was:
Golden
It was quite hard to find anything on Google related to this specific error. Ultimately, I found the corresponding GitHub ticket on the golden_toolkit repository, and the workaround: https://github.com/eBay/flutter_glove_box/issues/31#issuecomment-598420555. You have to create an empty assets folder.