Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions example/lib/widgets/date_text.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:clock/clock.dart';
import 'package:flutter/material.dart';

class DateText extends StatelessWidget {
const DateText({
super.key,
});

@override
Widget build(BuildContext context) {
return Text(
clock.now().toIso8601String(),
textAlign: TextAlign.center,
);
}
}
1 change: 1 addition & 0 deletions example/lib/widgets/widgets.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'contact_list_tile.dart';
export 'date_text.dart';
export 'red_button.dart';
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ environment:
sdk: ">=2.17.0 <3.0.0"

dependencies:
clock: ^1.1.2
flutter:
sdk: flutter

Expand Down
25 changes: 25 additions & 0 deletions example/test/widgets/custom_wrapper_golden_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:alchemist/alchemist.dart';
import 'package:clock/clock.dart';
import 'package:example/widgets/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('Custom Wrapper Golden Tests', () {
goldenTest(
'renders correctly',
fileName: 'custom_wrapper',
testWrapper: (test) async => withClock<void>(
Clock.fixed(DateTime.utc(2025, 9, 10)),
test,
),
builder: () => GoldenTestGroup(
children: [
GoldenTestScenario(
name: 'now',
child: const DateText(),
),
],
),
);
});
}
31 changes: 26 additions & 5 deletions lib/src/golden_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ Future<void> loadFonts() async {
/// built-in interaction receives a finder indicating all of the widgets
/// that should be interacted with.
///
/// The [testWrapper] is needed to wrap the test zone to another during the test
/// For example, when you need to use `runWithClock` inside goldens
/// ```dart
/// goldenTest(
/// ...
/// wrapper: (callback) async => withClock<void>(
/// Clock.fixed(DateTime(2025, 12, 10)),
/// callback,
/// )
/// ```
///
/// **Note**: If a built-in [whilePerforming] interaction is provided, the
/// widget tree is **always** pumped at least once before the assertion phase
/// of the test.
Expand All @@ -149,6 +160,7 @@ Future<void> goldenTest(
PumpAction pumpBeforeTest = onlyPumpAndSettle,
PumpWidget pumpWidget = onlyPumpWidget,
Interaction? whilePerforming,
Future<void> Function(Future<void> Function() callback)? testWrapper,
}) async {
if (skip) return;

Expand All @@ -173,10 +185,11 @@ Future<void> goldenTest(
await goldenTestAdapter.testWidgets(
description,
(tester) async {
final variantConfig = variant.currentConfig;
await goldenTestRunner.run(
tester: tester,
goldenPath: await variantConfig.filePathResolver(
Future<void> callback() async {
final variantConfig = variant.currentConfig;
await goldenTestRunner.run(
tester: tester,
goldenPath: await variantConfig.filePathResolver(
fileName,
variantConfig.environmentName,
),
Expand All @@ -192,7 +205,15 @@ Future<void> goldenTest(
pumpBeforeTest: pumpBeforeTest,
pumpWidget: pumpWidget,
whilePerforming: whilePerforming,
);
);
}

if (testWrapper != null) {
await testWrapper(callback);
} else {
await callback();
}

},
tags: tags,
variant: variant,
Expand Down