Skip to content

Releases

snaptest 0.1.0

Choose a tag to compare

@lolgab lolgab released this 14 Nov 13:00
42b5c32

Snapshot testing for Scala: record what your tests produce, review it as code, and catch changes as diffs.

Snapshots are stored as generated Scala sources in snapshots/ next to each test, so build tools rerun tests when a snapshot changes, and failures link to the snapshot's line.

Documentation: https://lolgab.github.io/snaptest/

Test frameworks

Framework Artifact Mix in
munit snaptest-munit MunitSnapshots
ScalaTest snaptest-scalatest ScalaTestSnapshots, AsyncScalaTestSnapshots
uTest snaptest-utest UTestSnapshots
specs2 snaptest-specs2 Specs2Snapshots
munit-cats-effect snaptest-munit-cats-effect CatsEffectSnapshots
weaver snaptest-weaver WeaverSnapshots
ZIO Test snaptest-zio ZioSnapshots

Every artifact is published for Scala 3.9+, on the JVM, Scala.js 1 and Scala Native 0.5. snaptest-core holds the framework-independent parts.

// sbt
libraryDependencies += "com.github.lolgab" %%% "snaptest-munit" % "0.1.0" % Test
// Mill
def mvnDeps = Seq(mvn"com.github.lolgab::snaptest-munit::0.1.0")
// Scala CLI
//> using test.dep com.github.lolgab::snaptest-munit::0.1.0

Highlights

  • assertSnapshot in any test, several times per test, keyed by test name; idiomatic APIs for each framework, like assertIOSnapshot for munit-cats-effect, expectSnapshot returning IO[Expectations] for weaver and assertZIOSnapshot for ZIO Test
  • Values rendered with pprint, strings stored verbatim, readable literals (plain, triple quotes or stripMargin); customizable with SnapshotRender
  • SNAPTEST=auto|always|prune|no, defaulting to no on CI so snapshots are never recorded silently
  • Failures show a diff and link to the snapshot with path:line
  • Snapshots of deleted tests are removed when every test of a suite ran and passed; with specs2, weaver and ZIO Test, use SNAPTEST=prune on a full run
  • Works with sbt, Mill and Scala CLI

Known limitations

  • With Mill, Scala.js and Scala Native tests see the environment of the Mill daemon: run ./mill --no-daemon when setting SNAPTEST
  • A weaver suite defining its own sharedResource, or a ZIO spec overriding aspects, keeps the snapshot hooks explicitly (see the guide)