Skip to content

Commit 31c96d3

Browse files
committed
README
1 parent 8c3c37c commit 31c96d3

File tree

2 files changed

+67
-33
lines changed

2 files changed

+67
-33
lines changed

README.md

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,51 @@
1-
```scala
2-
import munit.internal.difflib.Diffs
3-
import munit.Assertions
4-
5-
inline def assertSnapshot(inline name: String, contents: String) =
6-
Snapshots(name) match
7-
case None =>
8-
Snapshots.write(
9-
name,
10-
contents,
11-
Diffs.create(contents, "").createDiffOnlyReport()
12-
)
13-
14-
Assertions.fail(
15-
s"No snapshot was found for $name, please run checkSnapshots command and accept a snapshot for this test"
16-
)
17-
18-
case Some(value) =>
19-
val diff = Diffs.create(contents, value)
20-
if !diff.isEmpty then
21-
val diffReport = diff.createDiffOnlyReport()
22-
Snapshots.write(name, contents, diffReport)
23-
Assertions.assertNoDiff(contents, value)
24-
else
25-
Snapshots.clear(name)
26-
```
1+
<!--toc:start-->
2+
- [Installation (SBT)](#installation-sbt)
3+
- [Usage](#usage)
4+
<!--toc:end-->
5+
[![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)
6+
7+
## snapshots-testing
8+
9+
This is a micro library to aid with [snapshot testing](https://jestjs.io/docs/snapshot-testing).
10+
11+
The meat of the project is actually in build tool integration - I wanted to
12+
have an experience similar to that of [Insta](https://insta.rs/docs/cli/) with
13+
its Cargo integration. Currently only SBT is supported, but Mill support
14+
can be added easily, as the project is already structured in a way that favours that.
15+
16+
17+
## Installation (SBT)
18+
To add the plugin to your SBT build:
19+
20+
1. `addSbtPlugin("com.indoorvivants.snapshots" % "sbt-snapshots" % "VERSION")` to your `project/plugins.sbt` (see VERSION on the badge above)
21+
2. In the project where you would like to use snapshot testing, add these settings:
22+
23+
```scala
24+
.settings(
25+
snapshotsPackageName := "example",
26+
)
27+
.enablePlugins(SnapshotsPlugin)
28+
```
29+
30+
Package name is the only required setting, because the plugin will
31+
generate a source file that ties build-time filesystem locations with
32+
runtime test execution.
33+
34+
The library provides no diffing capabilities, instead delegating that
35+
task to the test framework of choice.
36+
37+
## Usage
38+
39+
To interact with snapshots, the following build tasks are provided:
40+
41+
- `snapshotsCheck` - interactively accept modified snapshots (if there are any)
42+
- `snapshotsAcceptAll` - accept all modified snapshots
43+
- `snapshotsDiscardAll` - discard all snapshot changes
44+
45+
46+
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.
47+
48+
[Sample MUnit integration](modules/example/src/test/scala/MunitSnapshotsIntegration.scala) |
49+
[Sample MUnit tests](modules/example/src/test/scala/MunitExampleTests.scala)
50+
51+

modules/snapshots-sbt-plugin/src/main/scala/SnapshotsPlugin.scala

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,22 @@ import SnapshotsBuild.SnapshotAction
2727

2828
object SnapshotsPlugin extends AutoPlugin {
2929
object autoImport {
30-
val snapshotsProjectIdentifier = settingKey[String]("")
31-
val snapshotsPackageName = settingKey[String]("")
32-
val snapshotsAddRuntimeDependency = settingKey[Boolean]("")
33-
val snapshotsTemporaryDirectory = settingKey[File]("")
34-
val snapshotsCheck = taskKey[Unit]("")
35-
val snapshotsAcceptAll = taskKey[Unit]("")
36-
val snapshotsDiscardAll = taskKey[Unit]("")
30+
val snapshotsProjectIdentifier = settingKey[String](
31+
"Project identifier to separate snapshots coming from cross-platform projects"
32+
)
33+
val snapshotsPackageName = settingKey[String](
34+
"Package name under which the generated Snapshots object will be created"
35+
)
36+
val snapshotsAddRuntimeDependency = settingKey[Boolean](
37+
"Whether to add snapshot runtime to the build - true by default, you shouldn't need to touch this"
38+
)
39+
val snapshotsTemporaryDirectory =
40+
settingKey[File]("Temp folder where snapshot diffs will be created")
41+
val snapshotsCheck =
42+
taskKey[Unit]("Interactively accept modified snapshotsAcceptAll")
43+
val snapshotsAcceptAll = taskKey[Unit]("Accept all modified snapshots")
44+
val snapshotsDiscardAll =
45+
taskKey[Unit]("Discard all modifications to snapshots")
3746
}
3847

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

0 commit comments

Comments
 (0)