Skip to main content

Inline snapshots

assertInlineSnapshot controls data defined in your source code.

You can use it to update values, such as case classes or sealed traits.

case class Config(environment: String, region: String)
val found = Config(environment = "dev", region = "us-east-2")

assertInlineSnapshot(found, Config(environment = "prod", region = "us-east-1"))

After running sbt test and snapshot4sPromote, the data will be updated.

case class Config(environment: String, region: String)
val found = Config(environment = "dev", region = "us-east-2")

assertInlineSnapshot(found, Config(environment = "dev", region = "us-east-2"))

You can also use it for strings.

assertInlineSnapshot(found = "The answer", "The answer")

If you have a long string, consider using file snapshots instead.

New snapshots

Use ??? to create a new snapshot:

case class Config(environment: String, region: String)
val found = Config(environment = "dev", region = "us-east-2")

assertInlineSnapshot(found, ???)

Promote your snapshots and see snapshot4s fill in the blanks:

sbt test
sbt snapshot4sPromote
case class Config(environment: String, region: String)
val found = Config(environment = "dev", region = "us-east-2")

assertInlineSnapshot(found, Config(environment = "dev", region = "us-east-2"))

Supported data types

The assertInlineSnapshot assertion will work for products and case classes as well as strings and primitive types.

assertInlineSnapshot(found = 1, 1)
assertInlineSnapshot(found = "The answer", "The answer")

case class Person(name: String)
assertInlineSnapshot(found = Person("Alice"), Person("Alice"))
assertInlineSnapshot(found = Nil, List(Person("Alice"), Person("Bob")))

Unsupported data types

It should not be used on variables.

val personVariable = Person("Bob") 
assertInlineSnapshot(found = Person("Alice"), personVariable) // Bad: personVariable will be replaced with Person("Alice")
assertInlineSnapshot(found = Person("Alice"), Person("Bob")) // Good: "Bob" will be replaced with "Alice".

It fails to compile for values that can't be represented as source code.

assertInlineSnapshot(found = new Object(), new Object())
// error:
// Cannot derive Repr instance. The type parameter is neither a Sum nor Product type.
//
// See the guide for a list of supported types:
// https://siriusxm.github.io/snapshot4s/inline-snapshots/#supported-data-types
// assertInlineSnapshot(found = new Object(), new Object())
// ^