snapshot/README.md

93 lines
3.1 KiB
Markdown
Raw Permalink Normal View History

2023-02-20 18:21:40 +00:00
# @datashard/snapshot
2023-02-20 18:17:38 +00:00
2024-04-25 12:09:41 +00:00
> Adds JSON Snapshot comparison to Cypress
2023-02-20 18:17:38 +00:00
2024-04-25 12:09:41 +00:00
## ⚠️ Breaking Changes ⚠️
- With V3, the `readFileMaybe` task has been removed, we now rely on `cy.fixture` internally.
- the previous `SNAPSHOT_UPDATE` Environment/Config Variable has been changed to `updateSnapshots`
> [!DANGER]
This means that previous tests will likely be broken, *please make sure that your tests pass before updating to the latest version of this Plugin*.
This current release will be released as `3.0.0-beta`, should Bugs be found by me or my Employer, I will open Issues/PRs to fix those, should anyone else find Bugs/Edgecases, etc. please open an Issue.
2023-02-20 18:17:38 +00:00
## Install
2024-04-25 12:09:41 +00:00
2024-01-31 18:49:52 +00:00
Requires Node 16 or above
2023-02-20 18:17:38 +00:00
```sh
2024-01-31 18:49:52 +00:00
npm i --save-dev @datashard/snapshot
2023-02-20 18:17:38 +00:00
```
## Import
2024-04-25 12:09:41 +00:00
2024-01-31 18:49:52 +00:00
After Installing, you'll need to add the following import into your Commands/Support File
2024-04-25 12:09:41 +00:00
2024-01-31 18:49:52 +00:00
> by default this will be `cypress/support/e2e.js`
2023-02-20 18:17:38 +00:00
```js
2024-01-31 18:49:52 +00:00
require('@datashard/snapshot').regsiter()
2023-02-20 18:17:38 +00:00
```
2023-02-20 18:21:40 +00:00
2024-04-25 12:09:41 +00:00
This will register a new Command `.snapshot()`, to create and compare JSON Snapshots.
2023-02-20 18:17:38 +00:00
2024-01-31 18:49:52 +00:00
## Config
2024-04-25 12:09:41 +00:00
2024-01-31 18:49:52 +00:00
You can pass `updateSnapshots` and `useFolders` as options in the `cypress.config.js` file
2023-02-20 18:17:38 +00:00
2024-03-28 09:44:10 +00:00
![Example Settings for the Module](./.github/assets/config.png)
2024-01-31 18:49:52 +00:00
Alternatively, you can also add `snapshotUpdate` as an Environment Variable to update your snapshots.
2023-02-20 18:17:38 +00:00
2024-01-31 18:49:52 +00:00
Simply pass `--env updateSnapshots=true` when running Cypress.
2023-02-20 18:17:38 +00:00
2024-01-31 18:49:52 +00:00
## Usage
2023-02-20 18:17:38 +00:00
If properly added, usage of this plugin is rather simple, just add `.snapshot()` to cypress functions that return valid JSON. (i.e. `cy.wrap`)
2024-01-31 18:49:52 +00:00
2024-04-25 12:09:41 +00:00
When properly added, you can chain `.snapshot()` off of `cy` functions like `cy.wrap`, just make sure they return valid JSON.
2024-01-31 18:49:52 +00:00
### Example
2024-04-25 12:09:41 +00:00
2023-02-20 18:17:38 +00:00
```js
2024-04-25 12:09:41 +00:00
describe("my test", () => {
2023-02-20 18:17:38 +00:00
it("works", () => {
cy.log("first snapshot");
cy.wrap({ foo: 42 }).snapshot("foo");
cy.log("second snapshot");
cy.wrap({ bar: 101 }).snapshot("bar");
});
});
```
2024-04-25 12:09:41 +00:00
This Plugin will then save your snapshots as
2024-01-31 18:49:52 +00:00
```ts
// useFolders: false
2024-04-25 12:09:41 +00:00
cypress/fixtures/my-test__works__foo.json
cypress/fixtures/my-test__works__bar.json
2023-02-20 18:17:38 +00:00
2024-01-31 18:49:52 +00:00
// useFolders: true
2024-04-25 12:09:41 +00:00
cypress/fixtures/my-test/works/foo.json
cypress/fixtures/my-test/works/bar.json
2023-02-20 18:17:38 +00:00
2024-04-25 12:09:41 +00:00
// {fixtureFolder}/<Context>-<Describe>-<It>-<Name?>.json
// {fixtureFolder}/<Context>/<Describe>/<It>/<Name?>.json
2023-02-20 18:17:38 +00:00
```
2024-04-25 12:09:41 +00:00
Snapshots will generally be saved using this the Convention mentioned in the Comment of the above Codeblock, which is provided by the named Cypress Test Steps.
Passing a name to the Snapshot function is required, but not checked, if you want to take multiple snapshots in the same block.
2023-02-20 18:17:38 +00:00
2024-04-25 12:09:41 +00:00
If you have two or more Snapshots in the same Block, the next one ***WILL*** always overwrite the previous one while updating, causing the First Snapshot in the Block to Fail.
While running your Tests, if a value changed, it will, of course, no longer match the snapshot and throw an Error.
2024-04-25 12:09:41 +00:00
Which will look like this:
2023-02-20 18:17:38 +00:00
2024-03-28 09:44:10 +00:00
![](./.github/assets/Error.png)
When the Test succeeds, it will instead log a Success in the Log and let you know where the File has been saved to, relative to the Fixture Snapshot Folder
![](./.github/assets/Correct.png)