Skip to content

Testing

Archipelago provides a complete testing setup out of the box — unit tests, widget tests, and integration tests with Patrol.

Test Structure

apps/template_app/
  test/
    flutter_test_config.dart          # Global unit/widget test config
    src/
      ...                             # Unit & widget tests
  integration_test/
    flutter_test_config.dart          # Global integration test config
    integration_test_driver.dart      # Driver for vanilla flutter test
    patrol_config.dart                # Patrol test configuration
    app_test.dart                     # Smoke test — verifies app launches
  patrol.toml                         # Patrol CLI configuration

Unit & Widget Tests

Every package has its own test/ directory. Run all tests across the workspace:

bash
melos run test

Run tests for a specific package:

bash
melos run test:select

flutter_test_config.dart

The test/flutter_test_config.dart file is automatically loaded before any test runs. Use it for global setup:

dart
Future<void> testExecutable(FutureOr<void> Function() testMain) async {
  TestWidgetsFlutterBinding.ensureInitialized();
  // Add global setup here (font loading, default screen size, etc.)
  await testMain();
}

Integration Tests with Patrol

Patrol provides native-aware integration testing for Flutter. It can interact with system dialogs, permissions, and native UI elements.

Install Patrol CLI

bash
dart pub global activate patrol_cli

Run Integration Tests

bash
# Run with melos
melos run integration-test

# Run all integration tests
melos run integration-test:all

# Run directly with Patrol
cd apps/template_app
patrol test --flavor development --target integration_test/app_test.dart

Writing Tests

dart
import 'package:patrol/patrol.dart';

import 'patrol_config.dart';

void main() {
  patrolTest(
    'app launches successfully',
    config: patrolTestConfig,
    ($) async {
      // Verify the app renders
      expect($('Archipelago'), findsOneWidget);

      // Interact with native elements
      await $.native.grantPermissionWhenInUse();
    },
  );
}

Configuration

patrol.toml — Patrol CLI settings:

toml
[android]
packageName = "com.example.template_app"

[ios]
bundleId = "com.example.templateApp"

[patrol]
testDir = "integration_test"

patrol_config.dart — Shared test config:

dart
const patrolTestConfig = PatrolTestConfig(
  findTimeout: Duration(seconds: 10),
);

Running Without Patrol

For CI environments without Patrol, use the standard Flutter integration test driver:

bash
flutter test integration_test/app_test.dart

Golden Tests (Visual Regression)

Golden tests validate your design system tokens visually. Archipelago uses Alchemist for golden testing — it renders widgets into images and compares them against baseline snapshots.

What's Tested

The UI kit includes golden tests for all design tokens:

FoundationWhat it validates
ColorsBackground, foreground, action, outline colors in light & dark themes
TypographyDisplay, heading, body, label, code styles in both themes
Spacing12-step spacing scale (none → xl6) as sized bars
RadiusAll 8 radius tokens applied to containers
ShadowsShadow tokens comparison
ButtonsAll styles, sizes, states, icons across themes
CardsVariants, sizes, interactive states
Text FieldsStates, sizes, icons, variants

Sample Golden Output

Foundations

Color Tokens:

Color Tokens

Semantic Colors (Light vs Dark):

Semantic Colors

Typography:

Typography

Individual Typography Categories

Display

Display

Heading

Heading

Body

Body

Label

Label

Button

Button

Code

Code

Spacing Scale:

Spacing

Radius Scale:

Radius

Shadows:

Shadows

Components

Button Styles:

Buttons

More Button Goldens

Filled Style

Filled

Outlined Style

Outlined

Text Style

Text

Sizes

Sizes

States

States

With Icons

Icons

Expanded

Expanded

Card Overview:

Cards

More Card Goldens

Variants

Variants

Sizes

Sizes

Interactive

Interactive

Text Fields:

TextField Overview

More TextField Goldens

Variants

Variants

Sizes

Sizes

States

States

Icons

Icons

Password Fields:

Password Overview

More Password Field Goldens

Variants

Variants

States

States

Visibility Toggle

Toggle

Strength Indicator

Strength

Running Golden Tests

Generate baseline images (first time or after intentional changes):

bash
melos run golden:update

Verify against baselines (CI or after code changes):

bash
melos run golden:check

Writing a Golden Test

dart
import 'package:alchemist/alchemist.dart';
import 'package:flutter/material.dart';

import '../../util/app_test_wrapper.dart';

void main() {
  goldenTest(
    'button renders correctly',
    fileName: 'components/button_variants',
    builder: () => GoldenTestGroup(
      scenarioConstraints: BoxConstraints(maxWidth: 300),
      children: [
        GoldenTestScenario(
          name: 'primary filled',
          child: buildTestWrapper(
            child: AppButton(
              label: 'Submit',
              variant: ButtonVariant.primary,
            ),
          ),
        ),
        GoldenTestScenario(
          name: 'primary outlined',
          child: buildTestWrapper(
            child: AppButton(
              label: 'Cancel',
              variant: ButtonVariant.primary,
              type: ButtonType.outlined,
            ),
          ),
        ),
      ],
    ),
    tags: ['golden'],
  );
}

Test Wrapper

Use buildTestWrapper() to wrap widgets with the correct theme:

dart
Widget buildTestWrapper({
  required Widget child,
  bool isDark = false,
}) {
  return MaterialApp(
    theme: isDark ? AppTheme.dark() : AppTheme.light(),
    home: Scaffold(body: child),
  );
}

CI Considerations

Alchemist renders text as colored blocks in CI mode to avoid cross-platform font differences. This means golden files generated on macOS will also pass on Linux CI runners.

Golden File Structure

packages/app_ui_kit/test/
  foundations/
    colors/
      color_golden_test.dart
      goldens/                    # Generated golden images
    typography/
      typography_golden_test.dart
      goldens/
    spacing/
      spacing_golden_test.dart
      goldens/
    radius/
      radius_golden_test.dart
      goldens/
  util/
    app_test_wrapper.dart

Coverage

Run tests with coverage and check thresholds:

bash
melos run coverage:full

This runs the full pipeline: collect → combine → check thresholds.

See Test Coverage for detailed configuration.

Main App vs Widgetbook

Main AppWidgetbook
PurposeProduction applicationUI component catalog & preview
PlatformsAndroid, iOSAndroid, iOS, Linux, Windows
Has integration testsYes (Patrol)No
Has unit testsYesMinimal (snapshot tests)
Has CMakeNo (mobile only)Yes (desktop support for previewing)
Entry pointmain_<flavor>.dartmain.dart
DIFull GetIt setupMinimal (just UI components)

Why Widgetbook Has Desktop Support

The widgetbook app includes CMake configs for Linux and Windows so designers and developers can preview components on desktop without needing a mobile emulator. This is purely for development convenience — the production app remains mobile-only.

CMake Files

Desktop CMake files are located at:

apps/app_widgetbook/
  linux/
    CMakeLists.txt              # Linux build config
    flutter/CMakeLists.txt      # Flutter plugin loader
  windows/
    CMakeLists.txt              # Windows build config
    flutter/CMakeLists.txt      # Flutter plugin loader

These follow the standard Flutter desktop CMake structure and are generated automatically by the flutter_modular_monorepo brick.

Built by Banua Coder