Skip to content

Commit

Permalink
Extract snapshots integration
Browse files Browse the repository at this point in the history
  • Loading branch information
keynmol committed Feb 10, 2024
1 parent e1cf32c commit c068c7c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
28 changes: 0 additions & 28 deletions modules/example/src/test/scala/ExampleTests.scala
Expand Up @@ -7,34 +7,6 @@ import munit.Assertions
import munit.FunSuite
import example.Snapshots

// This is a sample integration for Munit
trait SnapshotsIntegration {
self: FunSuite =>
def assertSnapshot(name: String, contents: String) = {
Snapshots.read(name) match {
case None =>
Snapshots.recordChanges(
name,
contents,
Diffs.create(contents, "").createDiffOnlyReport()
)

Assertions.fail(
s"No snapshot was found for $name, please run checkSnapshots command and accept a snapshot for this test"
)

case Some(value) =>
val diff = Diffs.create(contents, value)
if (!diff.isEmpty) {
val diffReport = diff.createDiffOnlyReport()
Snapshots.recordChanges(name, contents, diffReport)
Assertions.assertNoDiff(contents, value)
} else
Snapshots.clearChanges(name)
}
}
}

class ExampleTests extends FunSuite with SnapshotsIntegration {
test("hello") {
assertSnapshot("my.snapshot", "hello?")
Expand Down
34 changes: 34 additions & 0 deletions modules/example/src/test/scala/SnapshotsIntegration.scala
@@ -0,0 +1,34 @@
import munit._
import com.indoorvivants.snapshots._
import munit.internal.difflib.Diffs
import munit.Assertions
import munit.FunSuite
import example.Snapshots

// This is a sample integration for Munit
trait SnapshotsIntegration {
self: FunSuite =>
def assertSnapshot(name: String, contents: String) = {
Snapshots.read(name) match {
case None =>
Snapshots.recordChanges(
name,
contents,
Diffs.create(contents, "").createDiffOnlyReport()
)

Assertions.fail(
s"No snapshot was found for $name, please run checkSnapshots command and accept a snapshot for this test"
)

case Some(value) =>
val diff = Diffs.create(contents, value)
if (!diff.isEmpty) {
val diffReport = diff.createDiffOnlyReport()
Snapshots.recordChanges(name, contents, diffReport)
Assertions.assertNoDiff(contents, value)
} else
Snapshots.clearChanges(name)
}
}
}

0 comments on commit c068c7c

Please sign in to comment.