widgetbook_golden_test 0.4.0
widgetbook_golden_test: ^0.4.0 copied to clipboard
Utility for running golden tests on Widgetbook use cases.
widgetbook_golden_test #
Automatically generate Flutter golden tests from Widgetbook use cases generated by the widgetbook_generator.
Overview #
This library automates the creation of golden tests for Flutter widgets using Widgetbook use cases. It leverages the generated directory files from widgetbook_generator to discover use cases and render them into golden images for visual regression testing.
It is built upon widgetbook_golden_test_core, which provides the underlying engine for rendering and mocking dependencies.
Note: This has been mainly tested with auto-generated directories. Manually editing or creating
*.directories.g.dartshould work as well but is not intended.
Usage #
- Generate your Widgetbook directories using
widgetbook_generator:
dart run build_runner build -d
- Import the directories file generated.
- Call
runWidgetbookGoldenTestsinside themainfunction of a test file.
import '../widgetbook/main.directories.g.dart';
void main() {
runWidgetbookGoldenTests(
nodes: directories,
properties: WidgetbookGoldenTestsProperties(),
);
}
- Generate the golden files by running:
flutter test <path-to-test-file> --update-goldens
Note: Replace
<path-to-test-file>with the actual path to your test file. If omitted, Flutter will run all tests in the project.
Features #
- Automatic Discovery: All Widgetbook use cases are automatically discovered and tested.
- Network Image Mocking: Handles network images for reliable tests. You can simulate loading and error states using
WidgetbookGoldenTestsProperties.defaultErrorImageUrlandWidgetbookGoldenTestsProperties.defaultLoadingImageUrl. - Knob Support: Supports Widgetbook knobs by mocking the internal state.
- Customization: Customize properties at the group level via
WidgetbookGoldenTestsPropertiesor per use-case usingWidgetbookGoldenTestBuilder. - Play Actions: Use
GoldenPlayActionwithinWidgetbookGoldenTestBuilderto capture snapshots after interactions (e.g., scrolling, tapping). - Widgetbook Addons: Apply and merge Widgetbook addons to test different themes, locales, and text scales.
How It Works #
- Discovery: Use cases are defined and auto-generated in the directories file by
widgetbook_generator. - Traversal: The
runWidgetbookGoldenTestsfunction traverses the provided Widgetbook nodes. - Mocking: It sets up a mocked environment using
goldenTestZoneRunner, which includes a mockedHttpClientfor network images. - Execution: Each use case is pumped into a
MockedWidgetbookCasewhich provides the necessaryWidgetbookScopeand theme context. - Verification: The
WidgetbookGoldenFlutterTestRendererusesflutter_test'sexpectLaterandmatchesGoldenFileto perform the visual comparison.
Customization #
WidgetbookGoldenTestsProperties #
Use this to configure global settings for your golden tests:
addons: Apply global addons.theme: Set a default theme.onTestError: Handle or ignore specific errors.networkImageResolver: Provide custom logic for loading network images.
Warning
If you call runWidgetbookGoldenTests multiple times within the same main function using different special URLs (error/loading), you may encounter conflicts because HttpOverrides is global. It is recommended to use separate test files (with separate main functions) in such cases to avoid potential hang-ups.
WidgetbookGoldenTestBuilder #
Wrap your use case builder with this to customize individual tests:
skip: Skip specific tests.addons: Override or merge addons for a specific case.goldenActions: Define a sequence of actions and snapshot points.
Credits #
- This library’s image network mocking implementation is based on mocktail_image_network by Felix Angelov.
Contributing #
Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.