|
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 | +[](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 | + |
0 commit comments