Skip to content

Commit

Permalink
README
Browse files Browse the repository at this point in the history
  • Loading branch information
keynmol committed Feb 10, 2024
1 parent 8c3c37c commit 31c96d3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 33 deletions.
77 changes: 51 additions & 26 deletions README.md
@@ -1,26 +1,51 @@
```scala
import munit.internal.difflib.Diffs
import munit.Assertions

inline def assertSnapshot(inline name: String, contents: String) =
Snapshots(name) match
case None =>
Snapshots.write(
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 then
val diffReport = diff.createDiffOnlyReport()
Snapshots.write(name, contents, diffReport)
Assertions.assertNoDiff(contents, value)
else
Snapshots.clear(name)
```
<!--toc:start-->
- [Installation (SBT)](#installation-sbt)
- [Usage](#usage)
<!--toc:end-->
[![sbt-snapshots Scala version support](https://index.scala-lang.org/indoorvivants/snapshot-testing/sbt-snapshots/latest.svg)](https://index.scala-lang.org/indoorvivants/snapshot-testing/sbt-snapshots)

## snapshots-testing

This is a micro library to aid with [snapshot testing](https://jestjs.io/docs/snapshot-testing).

The meat of the project is actually in build tool integration - I wanted to
have an experience similar to that of [Insta](https://insta.rs/docs/cli/) with
its Cargo integration. Currently only SBT is supported, but Mill support
can be added easily, as the project is already structured in a way that favours that.


## Installation (SBT)
To add the plugin to your SBT build:

1. `addSbtPlugin("com.indoorvivants.snapshots" % "sbt-snapshots" % "VERSION")` to your `project/plugins.sbt` (see VERSION on the badge above)
2. In the project where you would like to use snapshot testing, add these settings:

```scala
.settings(
snapshotsPackageName := "example",
)
.enablePlugins(SnapshotsPlugin)
```

Package name is the only required setting, because the plugin will
generate a source file that ties build-time filesystem locations with
runtime test execution.

The library provides no diffing capabilities, instead delegating that
task to the test framework of choice.

## Usage

To interact with snapshots, the following build tasks are provided:

- `snapshotsCheck` - interactively accept modified snapshots (if there are any)
- `snapshotsAcceptAll` - accept all modified snapshots
- `snapshotsDiscardAll` - discard all snapshot changes


At this point there is no OOTB test framework integrations, but they will come in the future - even though they will be very small and short and are easier copied into your project.

[Sample MUnit integration](modules/example/src/test/scala/MunitSnapshotsIntegration.scala) |
[Sample MUnit tests](modules/example/src/test/scala/MunitExampleTests.scala)


23 changes: 16 additions & 7 deletions modules/snapshots-sbt-plugin/src/main/scala/SnapshotsPlugin.scala
Expand Up @@ -27,13 +27,22 @@ import SnapshotsBuild.SnapshotAction

object SnapshotsPlugin extends AutoPlugin {
object autoImport {
val snapshotsProjectIdentifier = settingKey[String]("")
val snapshotsPackageName = settingKey[String]("")
val snapshotsAddRuntimeDependency = settingKey[Boolean]("")
val snapshotsTemporaryDirectory = settingKey[File]("")
val snapshotsCheck = taskKey[Unit]("")
val snapshotsAcceptAll = taskKey[Unit]("")
val snapshotsDiscardAll = taskKey[Unit]("")
val snapshotsProjectIdentifier = settingKey[String](
"Project identifier to separate snapshots coming from cross-platform projects"
)
val snapshotsPackageName = settingKey[String](
"Package name under which the generated Snapshots object will be created"
)
val snapshotsAddRuntimeDependency = settingKey[Boolean](
"Whether to add snapshot runtime to the build - true by default, you shouldn't need to touch this"
)
val snapshotsTemporaryDirectory =
settingKey[File]("Temp folder where snapshot diffs will be created")
val snapshotsCheck =
taskKey[Unit]("Interactively accept modified snapshotsAcceptAll")
val snapshotsAcceptAll = taskKey[Unit]("Accept all modified snapshots")
val snapshotsDiscardAll =
taskKey[Unit]("Discard all modifications to snapshots")
}

val snapshotsTag = ConcurrentRestrictions.Tag("snapshots-check")
Expand Down

0 comments on commit 31c96d3

Please sign in to comment.