View Source Heyya.SnapshotCase (heyya v0.8.0)

Heyya.SnapshotCase allows for fast snapshot testing of Phoenix components. Snapshot testing components is a fast and easy way to ensure that they work and produce what they expected to produce without having to hand write assertions.

Complex Tests Made Easy

Suppose you have a component button with a color and and icon. You want to test that the correct css class is applied to the whole dom tree. Instead assert that the dom tree is functionally equivalent to the expected output.

component_snapshot_test "Eiffel 65" do
  assigns = %{}

  ~H|<.button phx-click="click" class="bg-blue-500">Click me</.button>|
end

Change Tests Faster

Any changes to that would require changes to the test are easily updated by running the tests and updating the snapshots. This happens by setting the environment variable HEYYA_OVERRIDE to true or 1.

HEYYA_OVERRIDE=true mix test

Summary

Functions

Wire up the module to prepare for snapshot testing.

A named component snapshot test

A named component snapshot test, where context is passed through.

Gets the directory to store snapshot files for the test.

Gets the stored snapshot for the given macro environment.

Functions

Link to this macro

__using__(opts)

View Source (macro)

Wire up the module to prepare for snapshot testing.

Link to this function

compare_html(snapshot_value, rendered_value)

View Source
@spec compare_html(binary(), binary()) :: boolean()
Link to this macro

component_snapshot_test(name, list)

View Source (macro)

A named component snapshot test

Link to this macro

component_snapshot_test(name, context, list)

View Source (macro)

A named component snapshot test, where context is passed through.

@spec directory(Macro.Env.t()) :: String.t()

Gets the directory to store snapshot files for the test.

Parameters

  • env: The macro environment which contains metadata about the test file.

Returns

The directory path as a binary where snapshots should be stored.

@spec filename(Macro.Env.t()) :: String.t()
@spec get_snapshot(Macro.Env.t()) :: binary()

Gets the stored snapshot for the given macro environment.

This looks up the snapshot file path and name based on the environment, reads the file contents, and returns the snapshot.

If there is no snapshot file yet, returns an empty binary.

Parameters

  • env: The macro environment which contains metadata about the test module and function.

Returns

The stored snapshot as a binary, or empty binary if no snapshot exists.

Link to this macro

inner_test(expr)

View Source (macro)
@spec override!(Macro.Env.t(), binary()) :: :ok
@spec override?() :: boolean()