Git Product home page Git Product logo

snapshot-testing's Introduction

sbt-snapshots Scala version support

snapshots-testing

This is a micro library to aid with snapshot testing.

The meat of the project is actually in build tool integration - I wanted to have an experience similar to that of Insta 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.

One of the goals was being able to work with Scala.js and Scala Native - so the Scala.js portion of the runtime includes small Node.js bindings to the fs module for filesystem operations.

The runtime is published for 2.12, 2.13, 3 and JVM, JS, Native.

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:

    .settings(
      snapshotsPackageName          := "example",
      snapshotsIntegrations         += SnapshotIntegration.MUnit // if using MUnit
    )
    .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

SBT tasks:

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

SBT settings:

  • snapshotsIntegrations - list of test framework integrations to generate in test sources

    Instead of providing a separate dependency for each test framework, the plugin generates a single-file integration. This helps avoid the dependency hell of incompatible framework versions. It might seem weird at first, because it is, but I believe it's the only way to stay sane.

  • snapshotsPackageName - package name to use for generated file

  • snapshotsForceOverwrite - if set to true, snapshot assertions won't fail the tests, but instead they will directly overwrite snapshot contents with the new value.

    It is particularly useful in a setting where you want to only check snapshots on CI, but always update them locally without having to explicitly accept.

    Recommended configuration is therefore snapshotsForceOverwrite := !sys.env.contains("CI") - or whatever way you can detect that your code is running in CI

Sample MUnit tests

You can see what the workflow looks like by

  1. Modifying the sample test above
  2. Running example/test and observing a failure
  3. Running example/snapshotsCheck and accepting a diff
  4. Running example/test again and it should succeed
  5. The snapshot file will be changed on disk

Here's the same workflow in video format:

video.mp4

Scala.js

The snapshots runtime will only run under Node.js (or any runtime where require("node:fs") import can succeed).

To make sure module import is enabled, you should ensure you're using the correct module kind - for example commonJS:

scalaJSLinkerConfig ~= (_.withModuleKind(ModuleKind.CommonJSModule))

Breaking changes log

0.0.7

The generated MUnit integration will target MUnit 1.0.0 and above, prompted by the changes in the packages for framework's difflib: https://github.com/scalameta/munit/pull/756/files#diff-a09dc8368568b397ac3069018fd17ad22e4f90c5e5cfdc12a4340f9d7c10ac09

All users are advised to migrate to MUnit 1.0.0+. As a consequence, following version bumps occurred:

  • Scala Native 0.4 -> 0.5
  • Scala.js 1.16
  • Scala 2.13.12 -> 2.13.14

0.0.6

Snapshots moved from src/test/resources/snapshots to src/snapshots. This was done because writing to test/resources re-triggers file watching when you run ~test.

You can return old behaviour by manually configuring new setting snapshotsLocation.

You can automatically move all the snapshots by running snapshotsMigrate 0.0.6

snapshot-testing's People

Contributors

keynmol avatar

Stargazers

Martynas Mickevičius avatar  avatar Greg Dorrell avatar Jamie Willis avatar Olivier Mélois avatar

Watchers

 avatar

snapshot-testing's Issues

difflib moved since munit 1.0.0-RC1

Thanks for this great library! :) Having stayed at the python world for a while, I certainly miss snapshot testing in Scala.

While trying it out, I noticed that the code generated by the library does not compile after the changes made in munit 1.0.0-RC1: scalameta/munit#756

[error] -- [E008] Not Found Error: /Users/martynas/projects/rpi-mon/target/scala-3.4.2/src_managed/test/MUnitIntegration.scala:21:22 
[error] 21 |import munit.internal.difflib.Diffs
[error]    |       ^^^^^^^^^^^^^^^^^^^^^^

A workaround is to use munit 1.0.0-M12 for now.

Add a mill plugin

The snapshots-buildtime should have everything necessary to implement it.
Use SBT plugin for reference

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.