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
Suppressing dead code warnings
If your code fails to compile with dead code following this construct, add a nowarn annotation to your assertInlineSnapshot call.
assertInlineSnapshot(1, ???): @annotation.nowarn
You can remove this after promoting your snapshot.
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())
// ^
Updating snapshots
You can update snapshot values using sbt test and sbt snapshot4sPromote.
If your datatype changes in structure, you can use scalafix to reset your snapshots.
For example, suppose you wish to add a version field to Config:
case class Config(
environment: String,
region: String,
+ version: String
)
If you make the change as-is, your tests will fail to compile and you will not be able to update them with snapshot4sPromote.
You can update your snapshots efficiently using the following steps:
- Before making the code change, replace all snapshot values with
???. You can use theResetSnapshotsscalafix rule.sbt scalafixAll --rules=github:siriusxm/snapshot4s/ResetSnapshots - Make your code change.
- Your snapshot tests will compile. Regenerate snapshots with
sbt testandsbt snapshot4sPromote.