Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added autofailNewSnapshots to force Cypress to fail if no snapshot is found #228

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

mosofsky
Copy link

Added autofailNewSnapshots

New parameter "autofailNewSnapshots" default to false.

If true, automatically fail if no existing snapshot found.  This is useful for production builds to prevent tests from passing just because they're missing snapshots.

In order to cause a failure when no snapshot is found, both matchImageSnapshot and matchTextSnapshot need to return a result object that has result.passed=false and result.updated=false.  That way logMessage throws the Error to make Cypress report the test as a failure.

In order to cause result.passed=false and result.updated=false, we have introduced the new autofailNewSnapshots configuration parameter that does what we want, namely (note the syntax for expressing tests below is Gherkin):

Given autofailNewSnapshots is true
When no <snapshot_type> snapshot is found
Then the test should fail

Examples:
  | snapshot_type |
  | text          |
  | image         |

Also, deleted package-lock.json since we now use yarn.

Testing:

The following scenarios test the new functionality pretty well.  I tested a few key ones that I cared most about and recorded "// PASS" to indicate that they worked.

Given autofailNewSnapshots is false
When no <snapshot_type> snapshot is found
  And it actual matches the snapshot
Then the test should pass

Examples:
  | snapshot_type |
  | text          |  // PASS
  | image         |
Given autofailNewSnapshots is false
When <snapshot_type> snapshot is found
  And it actual matches the snapshot
Then the test should pass

Examples:
  | snapshot_type |
  | text          |
  | image         |
Given autofailNewSnapshots is false
When no <snapshot_type> snapshot is found
  And actual does not match the snapshot
Then the test should fail

Examples:
  | snapshot_type |
  | text          |  // PASS
  | image         |
Given autofailNewSnapshots is false
When <snapshot_type> snapshot is found
  And actual does not match the snapshot
Then the test should fail

Examples:
  | snapshot_type |
  | text          |
  | image         |
Given autofailNewSnapshots is true
When no <snapshot_type> snapshot is found
Then the test should fail

Examples:
  | snapshot_type |
  | text          |  // PASS
  | image         |
Given autofailNewSnapshots is true
When a <snapshot_type> snapshot is found
  And actual match the snapshot
Then the test should pass

Examples:
  | snapshot_type |
  | text          |  // PASS
  | image         |
Given autofailNewSnapshots is true
When a <snapshot_type> snapshot is found
  And actual does not match the snapshot
Then the test should fail

Examples:
  | snapshot_type |
  | text          |  // PASS
  | image         |

michaelosofsky added 10 commits December 12, 2022 19:18
Then I published to npm:
1. npm login
2. npm publish --access=public
This is a proof of concept to test out going through the full development and release process on this fork of https://github.com/meinaart/cypress-plugin-snapshots

Also added .idea to .gitignore
I ran `yarn import` based on reference 1.

References:
1. https://classic.yarnpkg.com/lang/en/docs/migrating-from-npm/
This is a proof of concept to test out `yarn link` (reference 1)

References:
1. https://classic.yarnpkg.com/lang/en/docs/cli/link/
New parameter "autofailNewSnapshots" default to false.

If true, automatically fail if no existing snapshot found.  This is useful for production builds to prevent tests of passing just because they're missing snapshots.

In order to cause a failure when no snapshot is found, both matchImageSnapshot.js and matchTextSnapshot.js need to return a result object that has result.passed=false and result.updated=false.  That way logMessage.js throws the Error to make Cypress report the test as a failure.

In order to cause result.passed=false and result.updated=false, we have introduced the new autofailNewSnapshots configuration parameter that does what we want, namely:

```
Given autofailNewSnapshots is true
When no <snapshot_type> snapshot is found
Then the test should fail

Examples:
  | snapshot_type |
  | text          |
  | image         |
```

Also, deleted package-lock.json since we now use yarn.

Testing:

The following scenarios test the new functionality pretty well.  I tested a few key ones that I most about and recorded "// PASS" to indicate that they worked.

```
Given autofailNewSnapshots is false
When no <snapshot_type> snapshot is found
  And it actual matches the snapshot
Then the test should pass

Examples:
  | snapshot_type |
  | text          |  // PASS
  | image         |
```

```
Given autofailNewSnapshots is false
When <snapshot_type> snapshot is found
  And it actual matches the snapshot
Then the test should pass

Examples:
  | snapshot_type |
  | text          |
  | image         |
```

```
Given autofailNewSnapshots is false
When no <snapshot_type> snapshot is found
  And actual does not match the snapshot
Then the test should fail

Examples:
  | snapshot_type |
  | text          |  // PASS
  | image         |
```

```
Given autofailNewSnapshots is false
When <snapshot_type> snapshot is found
  And actual does not match the snapshot
Then the test should fail

Examples:
  | snapshot_type |
  | text          |
  | image         |
```

```
Given autofailNewSnapshots is true
When no <snapshot_type> snapshot is found
Then the test should fail

Examples:
  | snapshot_type |
  | text          |  // PASS
  | image         |
```

```
Given autofailNewSnapshots is true
When a <snapshot_type> snapshot is found
  And actual match the snapshot
Then the test should pass

Examples:
  | snapshot_type |
  | text          |  // PASS
  | image         |
```

```
Given autofailNewSnapshots is true
When a <snapshot_type> snapshot is found
  And actual does not match the snapshot
Then the test should fail

Examples:
  | snapshot_type |
  | text          |  // PASS
  | image         |
```
# Conflicts:
#	package-lock.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant