Skip to main content

Go-Snaps

Go-Snaps

Snapshot testing library for Go applications

Go-Snaps is a snapshot testing library for Go used in the Control Plane to verify that complex outputs match expected values.

Snapshot testing

Snapshot testing captures a component's output and compares it to a previously saved reference output (the "snapshot"). This approach is particularly useful for testing complex structures that are difficult to verify with traditional assertions.

Overviewโ€‹

Go-Snaps provides a simple, effective way to implement snapshot testing in Go applications. It's particularly valuable for testing complex data structures, API responses, and generated code.

Go-Snaps Logo

Why Go-Snaps?โ€‹

๐Ÿ“ธ Snapshot Capture

Automatically save output snapshots for future comparisons.

๐Ÿ”„ Snapshot Updates

Update snapshots when expected behavior changes.

๐Ÿ“Š Diff Generation

Clear visualization of differences between current output and snapshot.

๐Ÿงฉ Custom Serializers

Support for custom serialization of complex types.

โฑ๏ธ Inline Snapshots

Store small snapshots directly in test files.

๐Ÿ”ง CLI Integration

Command-line tools for managing snapshots.

Integration in the Control Planeโ€‹

Complex output testing

In the Control Plane, Go-Snaps is used to test components that produce complex outputs such as CRD generators, API responses, and resource templates.

The Control Plane uses Go-Snaps for testing various components:

API Responses

Testing the structure and content of API responses:

  • JSON response bodies
  • Error response formats
  • Pagination structures
  • Content negotiation results

Resource Templates

Verifying generated Kubernetes resources:

  • Deployment manifests
  • Service configurations
  • ConfigMap content
  • CRD definitions

Code Generation

Testing output of code generators:

  • Controller boilerplate
  • Client code
  • Schema validation
  • Documentation generation

Configuration Processing

Testing configuration transformation logic:

  • Config file parsing
  • Environment variable processing
  • Default value application
  • Validation error messages

Configurationโ€‹

Snapshot configuration and formats

Go-Snaps provides various configuration options and supports multiple data formats commonly used in the Control Plane.

Snapshot Storageโ€‹

By default, Go-Snaps stores snapshots in a __snapshots__ directory next to your test files:

pkg/controller/
โ”œโ”€โ”€ controller_test.go
โ”œโ”€โ”€ __snapshots__/
โ”‚ โ””โ”€โ”€ controller_test.snap
โ””โ”€โ”€ controller.go

Snapshots are stored in a readable format that's version control friendly.

Supported Formatsโ€‹

MatchJSON

Match JSON data with normalized formatting.

MatchYAML

Match YAML data with normalized formatting.

MatchInline

Store small snapshots directly in test files.

Match

Match any serializable data structure.

MatchText

Match text content without normalization.

MatchSnapshot

Low-level API for custom matching.

Advanced Featuresโ€‹

Custom Serializers

Define custom serialization for complex types

Update Mode

Update snapshots when behavior changes

Exclusion Paths

Exclude dynamic fields from comparison

Inline Snapshots

Store small snapshots directly in test files

CLI Usageโ€‹

Go-Snaps provides a command-line tool for managing snapshots:

# Install the CLI
go install github.com/gkampitakis/go-snaps/cmd/go-snaps@latest

# Show differences in snapshots
go-snaps diff

# Clean up unused snapshots
go-snaps cleanup

# Update all snapshots
go-snaps update

# Update specific snapshots
go-snaps update --test TestDeploymentController

Best Practices in the Control Planeโ€‹

Effective snapshot testing

The Control Plane follows these best practices for effective snapshot testing.

๐ŸŽฏ Test Focused Cases

Create separate snapshots for specific behaviors rather than large, catch-all snapshots.

๐Ÿ’พ Version Control Snapshots

Always commit snapshot files to version control alongside code changes.

๐Ÿงน Regular Cleanup

Use 'go-snaps cleanup' periodically to remove orphaned snapshots.

๐Ÿ” Code Review Snapshots

Review snapshot changes carefully during code reviews to catch unintended changes.

โฑ๏ธ Normalize Timestamps

Use custom serializers or exclusion paths to handle dynamic values like timestamps.

๐Ÿ“ Descriptive Test Names

Use clear, descriptive test names that explain what the snapshot represents.

Ginkgo

BDD-style testing framework for Go applications.

Gomega

Matcher/assertion library that works well with Go-Snaps.

Testify

Another testing toolkit used in the Control Plane.