Skip to content

Commit

Permalink
Add option to fail if snapshot files are missing
Browse files Browse the repository at this point in the history
To be used in CI - if we don't commit the snapshots, we're not testing anything.

Fixes #87
  • Loading branch information
lexanth committed Sep 29, 2021
1 parent 9cfb4b6 commit df2f147
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ Run Cypress with `--env updateSnapshots=true` in order to update the base image

Run Cypress with `--env failOnSnapshotDiff=false` in order to prevent test failures when an image diff does not pass.

### Requiring snapshots to be present

Run Cypress with `--env requireSnapshots=true` in order to fail if snapshots are missing. This is useful in continuous integration where snapshots should be present in advance.

### Reporter

Run Cypress with `--reporter cypress-image-snapshot/reporter` in order to report snapshot diffs in your test results. This can be helpful to use with `--env failOnSnapshotDiff=false` in order to quickly view all failing snapshots and their diffs.
Expand Down
5 changes: 5 additions & 0 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { MATCH, RECORD } from './constants';

const screenshotsFolder = Cypress.config('screenshotsFolder');
const updateSnapshots = Cypress.env('updateSnapshots') || false;
const requireSnapshots = Cypress.env('requireSnapshots') || false;
const failOnSnapshotDiff =
typeof Cypress.env('failOnSnapshotDiff') === 'undefined';

Expand Down Expand Up @@ -78,6 +79,10 @@ export function matchImageSnapshotCommand(defaultOptions) {
diffPixelCount,
diffOutputPath,
}) => {
if (added && requireSnapshots && !failOnSnapshotDiff) {
throw new Error(`New snapshot ${name} was added, but 'requireSnapshots' was set to true.
This is likely because this test was run in a CI environment in which snapshots should already be committed.`);
}
if (!pass && !added && !updated) {
const message = getErrorMessage(
diffSize,
Expand Down

0 comments on commit df2f147

Please sign in to comment.