mirror of
https://github.com/datashard/snapshot.git
synced 2024-12-22 01:47:35 +00:00
commit
This commit is contained in:
parent
6e0b81a522
commit
af82f108ad
31 changed files with 11056 additions and 11198 deletions
24
.eslintrc
24
.eslintrc
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"extends": [
|
||||
"plugin:cypress-dev/general"
|
||||
],
|
||||
"rules": {
|
||||
"comma-dangle": "off",
|
||||
"no-debugger": "warn"
|
||||
},
|
||||
"env": {
|
||||
"node": true
|
||||
}
|
||||
}
|
||||
{
|
||||
"extends": [
|
||||
"plugin:cypress-dev/general"
|
||||
],
|
||||
"rules": {
|
||||
"comma-dangle": "off",
|
||||
"no-debugger": "warn"
|
||||
},
|
||||
"env": {
|
||||
"node": true
|
||||
}
|
||||
}
|
||||
|
|
BIN
.github/assets/snapshot-mismatch.png
vendored
BIN
.github/assets/snapshot-mismatch.png
vendored
Binary file not shown.
Before Width: | Height: | Size: 84 KiB |
BIN
.github/assets/updated-mismatch.png
vendored
BIN
.github/assets/updated-mismatch.png
vendored
Binary file not shown.
Before Width: | Height: | Size: 41 KiB |
8
.gitignore
vendored
8
.gitignore
vendored
|
@ -1,4 +1,4 @@
|
|||
node_modules/
|
||||
.DS_Store
|
||||
npm-debug.log
|
||||
cypress/videos/
|
||||
node_modules/
|
||||
.DS_Store
|
||||
npm-debug.log
|
||||
cypress/videos/
|
||||
|
|
8
.npmrc
8
.npmrc
|
@ -1,4 +1,4 @@
|
|||
registry=http://registry.npmjs.org/
|
||||
save-exact=true
|
||||
progress=false
|
||||
package-lock=true
|
||||
registry=http://registry.npmjs.org/
|
||||
save-exact=true
|
||||
progress=false
|
||||
package-lock=true
|
||||
|
|
32
.travis.yml
32
.travis.yml
|
@ -1,16 +1,16 @@
|
|||
language: node_js
|
||||
notifications:
|
||||
email: true
|
||||
node_js:
|
||||
- 10
|
||||
|
||||
# Retry install on fail to avoid failing a build on network/disk/external errors
|
||||
install:
|
||||
- travis_retry npm ci
|
||||
|
||||
script:
|
||||
- npm run test
|
||||
- npm run cypress:run
|
||||
|
||||
after_success:
|
||||
- npm run semantic-release
|
||||
language: node_js
|
||||
notifications:
|
||||
email: true
|
||||
node_js:
|
||||
- 10
|
||||
|
||||
# Retry install on fail to avoid failing a build on network/disk/external errors
|
||||
install:
|
||||
- travis_retry npm ci
|
||||
|
||||
script:
|
||||
- npm run test
|
||||
- npm run cypress:run
|
||||
|
||||
after_success:
|
||||
- npm run semantic-release
|
||||
|
|
7
.vscode/settings.json
vendored
7
.vscode/settings.json
vendored
|
@ -1,7 +0,0 @@
|
|||
{
|
||||
"standard.enable": false,
|
||||
"eslint.enable": true,
|
||||
"eslint.autoFixOnSave": false,
|
||||
"git.ignoreLimitWarning": true,
|
||||
"standard.autoFixOnSave": false
|
||||
}
|
336
README.md
336
README.md
|
@ -1,168 +1,168 @@
|
|||
# @cypress/snapshot
|
||||
|
||||
> Adds value / object / DOM element snapshot testing support to Cypress test runner
|
||||
|
||||
[![NPM][npm-icon] ][npm-url]
|
||||
|
||||
[![Build status][ci-image] ][ci-url]
|
||||
[![semantic-release][semantic-image] ][semantic-url]
|
||||
[![renovate-app badge][renovate-badge]][renovate-app]
|
||||
|
||||
> **Note** \
|
||||
> \
|
||||
> Please take a look at a few other Cypress snapshot plugins:
|
||||
>
|
||||
> - [cypress-plugin-snapshots](https://github.com/meinaart/cypress-plugin-snapshots)
|
||||
> - [cypress-image-snapshot](https://github.com/palmerhq/cypress-image-snapshot).
|
||||
|
||||
## Install
|
||||
|
||||
Requires [Node](https://nodejs.org/en/) version 10 or above.
|
||||
|
||||
```sh
|
||||
npm install --save-dev @cypress/snapshot
|
||||
```
|
||||
|
||||
## Import
|
||||
|
||||
After installing, add the following to your `cypress/support/commands.js` file
|
||||
|
||||
```js
|
||||
require("@cypress/snapshot").register();
|
||||
```
|
||||
This registers a new command to create new snapshot or compare value to old snapshot
|
||||
|
||||
and add the following to your `cypress.config.js`
|
||||
|
||||
```js
|
||||
e2e: {
|
||||
setupNodeEvents(on, config) {
|
||||
require("@cypress/snapshot").tasks(on, config)
|
||||
},
|
||||
```
|
||||
|
||||
**Note:** `@cypress/snapshot` **requires** the `readFileMaybe` plugin to be included, which can be easily done using the code above
|
||||
|
||||
|
||||
# Usage
|
||||
|
||||
Currently, if you want to take more than one snapshot, you need to pass a Step Name to prevent overwrites / test failures
|
||||
|
||||
```js
|
||||
describe("my tests", () => {
|
||||
it("works", () => {
|
||||
cy.log("first snapshot");
|
||||
cy.wrap({ foo: 42 }).snapshot("foo");
|
||||
cy.log("second snapshot");
|
||||
cy.wrap({ bar: 101 }).snapshot("bar");
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
In the above case, you can find the stored snapshot in their own files, mentioned above them
|
||||
|
||||
```json
|
||||
// cypress/snapshots/my-tests-works-foo.json
|
||||
{"foo": 42}
|
||||
// cypress/snapshots/my-tests-works-bar.json
|
||||
{"bar": 101}
|
||||
```
|
||||
|
||||
If you change the site values, the saved snapshot will no longer match, throwing an error
|
||||
|
||||
( picture taken from `cypress/snapshots/Arrays.json`)
|
||||
![Snapshot mismatch](.github/assets/updated-mismatch.png)
|
||||
|
||||
Click on the `SNAPSHOT` step in the Command Log to see expected and current value printed in the DevTools.
|
||||
|
||||
### Options
|
||||
|
||||
You can control snapshot comparison and behavior through a few options.
|
||||
|
||||
```js
|
||||
cy.get(...).snapshot({
|
||||
snapshotName: 'Snapshot Name', // to use as a File Name
|
||||
snapshotPath: 'cypress/not_snapshots', // where to save the Snapshot
|
||||
json: false // convert DOM elements into JSON
|
||||
}) // when storing in the snapshot file
|
||||
|
||||
// will save as
|
||||
// cypress/not_snapshots/Snapshot-Name.json
|
||||
```
|
||||
|
||||
You can also pass a "Step Name" to the Function
|
||||
|
||||
```js
|
||||
cy.get(...).snapshot("Intercepted API Request")
|
||||
// will save as
|
||||
// cypress/snapshots/<context>-<describe>-<it>-Intercepted-API-Request.json
|
||||
// to prevent duplications
|
||||
```
|
||||
|
||||
or both
|
||||
|
||||
```js
|
||||
cy.get(...).snapshot("Intercepted API Request", {
|
||||
snapshotPath: "cypress/snapshots/api",
|
||||
snapshotName: "first_intercept"
|
||||
})
|
||||
|
||||
// will save as
|
||||
// cypress/snapshots/api/first_intercept.json
|
||||
```
|
||||
|
||||
### Configuration
|
||||
|
||||
This module provides some configuration options:
|
||||
|
||||
#### snapshotPath
|
||||
Sets the default Path for saving Snapshots (default: `cypress/snapshots`)
|
||||
|
||||
## Debugging
|
||||
|
||||
To debug this module run with environment variable `DEBUG=@cypress/snapshot`
|
||||
|
||||
#
|
||||
|
||||
### Small print
|
||||
|
||||
Author: Gleb Bahmutov <gleb@cypress.io> & Joshua D. <[data@shard.wtf](mailto:data@shard.wtf)> © Cypress.io 2017-2022
|
||||
<br>
|
||||
License: MIT - do anything with the code, but don't blame us if it does not work.
|
||||
|
||||
Support: If you find any problems with this module, email / tweet /
|
||||
[open issue](https://github.com/cypress-io/snapshot/issues) on Github
|
||||
|
||||
## MIT License
|
||||
|
||||
Copyright (c) 2017-2022 Cypress.io <hello@cypress.io>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation
|
||||
files (the "Software"), to deal in the Software without
|
||||
restriction, including without limitation the rights to use,
|
||||
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
[npm-icon]: https://nodei.co/npm/@cypress/snapshot.svg?downloads=true
|
||||
[npm-url]: https://npmjs.org/package/@cypress/snapshot
|
||||
[ci-image]: https://travis-ci.org/cypress-io/snapshot.svg?branch=master
|
||||
[ci-url]: https://travis-ci.org/cypress-io/snapshot
|
||||
[semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
|
||||
[semantic-url]: https://github.com/semantic-release/semantic-release
|
||||
[renovate-badge]: https://img.shields.io/badge/renovate-app-blue.svg
|
||||
[renovate-app]: https://renovateapp.com/
|
||||
# @cypress/snapshot
|
||||
|
||||
> Adds value / object / DOM element snapshot testing support to Cypress test runner
|
||||
|
||||
[![NPM][npm-icon] ][npm-url]
|
||||
|
||||
[![Build status][ci-image] ][ci-url]
|
||||
[![semantic-release][semantic-image] ][semantic-url]
|
||||
[![renovate-app badge][renovate-badge]][renovate-app]
|
||||
|
||||
> **Note** \
|
||||
> \
|
||||
> Please take a look at a few other Cypress snapshot plugins:
|
||||
>
|
||||
> - [cypress-plugin-snapshots](https://github.com/meinaart/cypress-plugin-snapshots)
|
||||
> - [cypress-image-snapshot](https://github.com/palmerhq/cypress-image-snapshot).
|
||||
|
||||
## Install
|
||||
|
||||
Requires [Node](https://nodejs.org/en/) version 10 or above.
|
||||
|
||||
```sh
|
||||
npm install --save-dev @cypress/snapshot
|
||||
```
|
||||
|
||||
## Import
|
||||
|
||||
After installing, add the following to your `cypress/support/commands.js` file
|
||||
|
||||
```js
|
||||
require("@cypress/snapshot").register();
|
||||
```
|
||||
This registers a new command to create new snapshot or compare value to old snapshot
|
||||
|
||||
and add the following to your `cypress.config.js`
|
||||
|
||||
```js
|
||||
e2e: {
|
||||
setupNodeEvents(on, config) {
|
||||
require("@cypress/snapshot").tasks(on, config)
|
||||
},
|
||||
```
|
||||
|
||||
**Note:** `@cypress/snapshot` **requires** the `readFileMaybe` plugin to be included, which can be easily done using the code above
|
||||
|
||||
|
||||
# Usage
|
||||
|
||||
Currently, if you want to take more than one snapshot, you need to pass a Step Name to prevent overwrites / test failures
|
||||
|
||||
```js
|
||||
describe("my tests", () => {
|
||||
it("works", () => {
|
||||
cy.log("first snapshot");
|
||||
cy.wrap({ foo: 42 }).snapshot("foo");
|
||||
cy.log("second snapshot");
|
||||
cy.wrap({ bar: 101 }).snapshot("bar");
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
In the above case, you can find the stored snapshot in their own files, mentioned above them
|
||||
|
||||
```json
|
||||
// cypress/snapshots/my-tests-works-foo.json
|
||||
{"foo": 42}
|
||||
// cypress/snapshots/my-tests-works-bar.json
|
||||
{"bar": 101}
|
||||
```
|
||||
|
||||
If you change the site values, the saved snapshot will no longer match, throwing an error
|
||||
|
||||
( picture taken from `cypress/snapshots/Arrays.json`)
|
||||
![Snapshot mismatch](.github/assets/updated-mismatch.png)
|
||||
|
||||
Click on the `SNAPSHOT` step in the Command Log to see expected and current value printed in the DevTools.
|
||||
|
||||
### Options
|
||||
|
||||
You can control snapshot comparison and behavior through a few options.
|
||||
|
||||
```js
|
||||
cy.get(...).snapshot({
|
||||
snapshotName: 'Snapshot Name', // to use as a File Name
|
||||
snapshotPath: 'cypress/not_snapshots', // where to save the Snapshot
|
||||
json: false // convert DOM elements into JSON
|
||||
}) // when storing in the snapshot file
|
||||
|
||||
// will save as
|
||||
// cypress/not_snapshots/Snapshot-Name.json
|
||||
```
|
||||
|
||||
You can also pass a "Step Name" to the Function
|
||||
|
||||
```js
|
||||
cy.get(...).snapshot("Intercepted API Request")
|
||||
// will save as
|
||||
// cypress/snapshots/<context>-<describe>-<it>-Intercepted-API-Request.json
|
||||
// to prevent duplications
|
||||
```
|
||||
|
||||
or both
|
||||
|
||||
```js
|
||||
cy.get(...).snapshot("Intercepted API Request", {
|
||||
snapshotPath: "cypress/snapshots/api",
|
||||
snapshotName: "first_intercept"
|
||||
})
|
||||
|
||||
// will save as
|
||||
// cypress/snapshots/api/first_intercept.json
|
||||
```
|
||||
|
||||
### Configuration
|
||||
|
||||
This module provides some configuration options:
|
||||
|
||||
#### snapshotPath
|
||||
Sets the default Path for saving Snapshots (default: `cypress/snapshots`)
|
||||
|
||||
## Debugging
|
||||
|
||||
To debug this module run with environment variable `DEBUG=@cypress/snapshot`
|
||||
|
||||
#
|
||||
|
||||
### Small print
|
||||
|
||||
Author: Gleb Bahmutov <gleb@cypress.io> & Joshua D. <[data@shard.wtf](mailto:data@shard.wtf)> © Cypress.io 2017-2022
|
||||
<br>
|
||||
License: MIT - do anything with the code, but don't blame us if it does not work.
|
||||
|
||||
Support: If you find any problems with this module, email / tweet /
|
||||
[open issue](https://github.com/cypress-io/snapshot/issues) on Github
|
||||
|
||||
## MIT License
|
||||
|
||||
Copyright (c) 2017-2022 Cypress.io <hello@cypress.io>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation
|
||||
files (the "Software"), to deal in the Software without
|
||||
restriction, including without limitation the rights to use,
|
||||
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
[npm-icon]: https://nodei.co/npm/@cypress/snapshot.svg?downloads=true
|
||||
[npm-url]: https://npmjs.org/package/@cypress/snapshot
|
||||
[ci-image]: https://travis-ci.org/cypress-io/snapshot.svg?branch=master
|
||||
[ci-url]: https://travis-ci.org/cypress-io/snapshot
|
||||
[semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
|
||||
[semantic-url]: https://github.com/semantic-release/semantic-release
|
||||
[renovate-badge]: https://img.shields.io/badge/renovate-app-blue.svg
|
||||
[renovate-app]: https://renovateapp.com/
|
||||
|
|
|
@ -1,27 +1,27 @@
|
|||
/* eslint-env mocha */
|
||||
/* global cy */
|
||||
describe("Random Describe", () => {
|
||||
context("Random Context", () => {
|
||||
it("Random It", () => {
|
||||
cy.fixture("File").snapshot("Fixture File", {
|
||||
humanName: "Random Fixture File"
|
||||
});
|
||||
// cy.fixture("File2").snapshot("Fixture File",
|
||||
});
|
||||
|
||||
// it("works with numbers", () => {
|
||||
// console.log(cy.wrap(42))
|
||||
// cy.wrap(42).snapshot();
|
||||
// });
|
||||
|
||||
// it("works with strings", () => {
|
||||
// console.log(cy.wrap("foo-bar"))
|
||||
// cy.wrap("foo-bar").snapshot();
|
||||
// });
|
||||
|
||||
// it("works with arrays", () => {
|
||||
// console.log(cy.wrap([1, 2, 3]))
|
||||
// cy.wrap([1, 2, 3]).snapshot();
|
||||
// });
|
||||
});
|
||||
});
|
||||
/* eslint-env mocha */
|
||||
/* global cy */
|
||||
describe("Random Describe", () => {
|
||||
context("Random Context", () => {
|
||||
it("Random It", () => {
|
||||
cy.fixture("File").snapshot("Fixture File", {
|
||||
humanName: "Random Fixture File"
|
||||
});
|
||||
// cy.fixture("File2").snapshot("Fixture File",
|
||||
});
|
||||
|
||||
// it("works with numbers", () => {
|
||||
// console.log(cy.wrap(42))
|
||||
// cy.wrap(42).snapshot();
|
||||
// });
|
||||
|
||||
// it("works with strings", () => {
|
||||
// console.log(cy.wrap("foo-bar"))
|
||||
// cy.wrap("foo-bar").snapshot();
|
||||
// });
|
||||
|
||||
// it("works with arrays", () => {
|
||||
// console.log(cy.wrap([1, 2, 3]))
|
||||
// cy.wrap([1, 2, 3]).snapshot();
|
||||
// });
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,33 +1,33 @@
|
|||
/* eslint-env mocha */
|
||||
/* global cy */
|
||||
describe("@cypress/snapshot", () => {
|
||||
context("simple types", () => {
|
||||
it("works with objects", () => {
|
||||
cy.fixture("File2").snapshot({
|
||||
snapshotPath: "cypress/snapshots",
|
||||
snapshotName: "Objects",
|
||||
});
|
||||
});
|
||||
|
||||
it("works with numbers", () => {
|
||||
cy.wrap(42).snapshot({
|
||||
snapshotPath: "cypress/snapshots",
|
||||
snapshotName: "Numbers",
|
||||
});
|
||||
});
|
||||
|
||||
it("works with strings", () => {
|
||||
cy.wrap("foo-bar").snapshot({
|
||||
snapshotPath: "cypress/snapshots",
|
||||
snapshotName: "Strings",
|
||||
});
|
||||
});
|
||||
|
||||
it("works with arrays", () => {
|
||||
cy.wrap([1, 2, 3]).snapshot({
|
||||
snapshotPath: "cypress/snapshots",
|
||||
snapshotName: "Arrays",
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
/* eslint-env mocha */
|
||||
/* global cy */
|
||||
describe("@cypress/snapshot", () => {
|
||||
context("simple types", () => {
|
||||
it("works with objects", () => {
|
||||
cy.fixture("File2").snapshot({
|
||||
snapshotPath: "cypress/snapshots",
|
||||
snapshotName: "Objects",
|
||||
});
|
||||
});
|
||||
|
||||
it("works with numbers", () => {
|
||||
cy.wrap(42).snapshot({
|
||||
snapshotPath: "cypress/snapshots",
|
||||
snapshotName: "Numbers",
|
||||
});
|
||||
});
|
||||
|
||||
it("works with strings", () => {
|
||||
cy.wrap("foo-bar").snapshot({
|
||||
snapshotPath: "cypress/snapshots",
|
||||
snapshotName: "Strings",
|
||||
});
|
||||
});
|
||||
|
||||
it("works with arrays", () => {
|
||||
cy.wrap([1, 2, 3]).snapshot({
|
||||
snapshotPath: "cypress/snapshots",
|
||||
snapshotName: "Arrays",
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
{
|
||||
"foo": "bar",
|
||||
"Fizzy Drink": "Soda"
|
||||
}
|
||||
{
|
||||
"foo": "bar",
|
||||
"Fizzy Drink": "Soda"
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
{
|
||||
"foo": "bar",
|
||||
"Fizzy Drink": "Pop"
|
||||
}
|
||||
{
|
||||
"foo": "bar",
|
||||
"Fizzy Drink": "Pop"
|
||||
}
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
// ***********************************************************
|
||||
// This example plugins/index.js can be used to load plugins
|
||||
//
|
||||
// You can change the location of this file or turn off loading
|
||||
// the plugins file with the 'pluginsFile' configuration option.
|
||||
//
|
||||
// You can read more here:
|
||||
// https://on.cypress.io/plugins-guide
|
||||
// ***********************************************************
|
||||
|
||||
// This function is called when a project is opened or re-opened (e.g. due to
|
||||
// the project's config changing)
|
||||
|
||||
module.exports = (on, config) => {
|
||||
// `on` is used to hook into various events Cypress emits
|
||||
// `config` is the resolved Cypress config
|
||||
}
|
||||
// ***********************************************************
|
||||
// This example plugins/index.js can be used to load plugins
|
||||
//
|
||||
// You can change the location of this file or turn off loading
|
||||
// the plugins file with the 'pluginsFile' configuration option.
|
||||
//
|
||||
// You can read more here:
|
||||
// https://on.cypress.io/plugins-guide
|
||||
// ***********************************************************
|
||||
|
||||
// This function is called when a project is opened or re-opened (e.g. due to
|
||||
// the project's config changing)
|
||||
|
||||
module.exports = (on, config) => {
|
||||
// `on` is used to hook into various events Cypress emits
|
||||
// `config` is the resolved Cypress config
|
||||
}
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
// register .snapshot() command
|
||||
require('../..').register()
|
||||
// register .snapshot() command
|
||||
require('../..').register()
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
// ***********************************************************
|
||||
// This example support/index.js is processed and
|
||||
// loaded automatically before your test files.
|
||||
//
|
||||
// This is a great place to put global configuration and
|
||||
// behavior that modifies Cypress.
|
||||
//
|
||||
// You can change the location of this file or turn off
|
||||
// automatically serving support files with the
|
||||
// 'supportFile' configuration option.
|
||||
//
|
||||
// You can read more here:
|
||||
// https://on.cypress.io/configuration
|
||||
// ***********************************************************
|
||||
|
||||
// Import commands.js using ES2015 syntax:
|
||||
import './commands'
|
||||
|
||||
// Alternatively you can use CommonJS syntax:
|
||||
// require('./commands')
|
||||
// ***********************************************************
|
||||
// This example support/index.js is processed and
|
||||
// loaded automatically before your test files.
|
||||
//
|
||||
// This is a great place to put global configuration and
|
||||
// behavior that modifies Cypress.
|
||||
//
|
||||
// You can change the location of this file or turn off
|
||||
// automatically serving support files with the
|
||||
// 'supportFile' configuration option.
|
||||
//
|
||||
// You can read more here:
|
||||
// https://on.cypress.io/configuration
|
||||
// ***********************************************************
|
||||
|
||||
// Import commands.js using ES2015 syntax:
|
||||
import './commands'
|
||||
|
||||
// Alternatively you can use CommonJS syntax:
|
||||
// require('./commands')
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
Thank you for taking time to open a new issue. Please answer a few questions to help us fix it faster. You can delete text that is irrelevant to the issue.
|
||||
|
||||
## Is this a bug report or a feature request?
|
||||
|
||||
If this is a bug report, please provide as much info as possible
|
||||
|
||||
- version
|
||||
- platform
|
||||
- expected behavior
|
||||
- actual behavior
|
||||
|
||||
If this is a new feature request, please describe it below
|
||||
Thank you for taking time to open a new issue. Please answer a few questions to help us fix it faster. You can delete text that is irrelevant to the issue.
|
||||
|
||||
## Is this a bug report or a feature request?
|
||||
|
||||
If this is a bug report, please provide as much info as possible
|
||||
|
||||
- version
|
||||
- platform
|
||||
- expected behavior
|
||||
- actual behavior
|
||||
|
||||
If this is a new feature request, please describe it below
|
||||
|
|
20872
package-lock.json
generated
20872
package-lock.json
generated
File diff suppressed because it is too large
Load diff
141
package.json
141
package.json
|
@ -1,70 +1,71 @@
|
|||
{
|
||||
"name": "cypress-snapshot",
|
||||
"description": "Adds value / object / DOM element snapshot testing support to Cypress test runner",
|
||||
"version": "1.0.0",
|
||||
"author": "Gleb Bahmutov <gleb@cypress.io>",
|
||||
"bugs": "https://github.com/cypress-io/snapshot/issues",
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
},
|
||||
"files": [
|
||||
"img",
|
||||
"src/*.js",
|
||||
"!src/*-spec.js"
|
||||
],
|
||||
"homepage": "https://github.com/cypress-io/snapshot#readme",
|
||||
"keywords": [
|
||||
"cypress",
|
||||
"cypress-io",
|
||||
"plugin",
|
||||
"snapshot",
|
||||
"testing"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "src/",
|
||||
"private": false,
|
||||
"publishConfig": {
|
||||
"registry": "http://registry.npmjs.org/"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/cypress-io/snapshot.git"
|
||||
},
|
||||
"scripts": {
|
||||
"ban": "ban",
|
||||
"deps": "deps-ok && dependency-check --no-dev .",
|
||||
"issues": "git-issues",
|
||||
"license": "license-checker --production --onlyunknown --csv",
|
||||
"lint": "eslint --fix src/*.js",
|
||||
"pretest": "npm run lint",
|
||||
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
|
||||
"test": "npm run unit",
|
||||
"unit": "mocha src/*-spec.js",
|
||||
"unused-deps": "dependency-check --unused --no-dev . --entry src/add-initial-snapshot-file.js",
|
||||
"postinstall": "echo 'no postinstall script'",
|
||||
"semantic-release": "semantic-release",
|
||||
"cypress:open": "cypress open",
|
||||
"cypress:run": "cypress run"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ban-sensitive-files": "1.9.15",
|
||||
"cypress": "10.6.0",
|
||||
"debug": "3.2.7",
|
||||
"dependency-check": "2.10.1",
|
||||
"deps-ok": "1.4.1",
|
||||
"eslint": "4.19.1",
|
||||
"eslint-plugin-cypress-dev": "1.1.2",
|
||||
"git-issues": "1.3.1",
|
||||
"license-checker": "15.0.0",
|
||||
"mocha": "6.2.3",
|
||||
"semantic-release": "17.4.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"@wildpeaks/snapshot-dom": "1.6.0",
|
||||
"check-more-types": "2.24.0",
|
||||
"js-beautify": "1.13.13",
|
||||
"lazy-ass": "1.6.0",
|
||||
"snap-shot-compare": "3.0.0",
|
||||
"snap-shot-store": "1.2.3"
|
||||
}
|
||||
}
|
||||
{
|
||||
"name": "cypress-snapshot",
|
||||
"description": "Adds value / object / DOM element snapshot testing support to Cypress test runner",
|
||||
"version": "1.0.1",
|
||||
"author": "Joshua D. <data@shard.wtf>, Gleb Bahmutov <gleb@cypress.io>",
|
||||
"bugs": "https://github.com/cypress-io/snapshot/issues",
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
},
|
||||
"files": [
|
||||
"img",
|
||||
"src/*",
|
||||
"src/*/**",
|
||||
"!src/*-spec.js"
|
||||
],
|
||||
"homepage": "https://github.com/cypress-io/snapshot#readme",
|
||||
"keywords": [
|
||||
"cypress",
|
||||
"cypress-io",
|
||||
"plugin",
|
||||
"snapshot",
|
||||
"testing"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "src/index.js",
|
||||
"private": false,
|
||||
"publishConfig": {
|
||||
"registry": "https://pkgs.dev.azure.com/CPCorporatePlanningAG/_packaging/webclient/npm/registry/"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/cypress-io/snapshot.git"
|
||||
},
|
||||
"scripts": {
|
||||
"ban": "ban",
|
||||
"deps": "deps-ok && dependency-check --no-dev .",
|
||||
"issues": "git-issues",
|
||||
"license": "license-checker --production --onlyunknown --csv",
|
||||
"lint": "eslint --fix src/*.js",
|
||||
"pretest": "npm run lint",
|
||||
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
|
||||
"test": "npm run unit",
|
||||
"unit": "mocha src/*-spec.js",
|
||||
"unused-deps": "dependency-check --unused --no-dev . --entry src/add-initial-snapshot-file.js",
|
||||
"postinstall": "echo 'no postinstall script'",
|
||||
"semantic-release": "semantic-release",
|
||||
"cypress:open": "cypress open",
|
||||
"cypress:run": "cypress run"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ban-sensitive-files": "1.9.15",
|
||||
"cypress": "10.6.0",
|
||||
"debug": "3.2.7",
|
||||
"dependency-check": "2.10.1",
|
||||
"deps-ok": "1.4.1",
|
||||
"eslint": "4.19.1",
|
||||
"eslint-plugin-cypress-dev": "1.1.2",
|
||||
"git-issues": "1.3.1",
|
||||
"license-checker": "15.0.0",
|
||||
"mocha": "6.2.3",
|
||||
"semantic-release": "17.4.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"@wildpeaks/snapshot-dom": "1.6.0",
|
||||
"check-more-types": "2.24.0",
|
||||
"js-beautify": "1.13.13",
|
||||
"lazy-ass": "1.6.0",
|
||||
"snap-shot-compare": "3.0.0",
|
||||
"snap-shot-store": "1.2.3"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,24 +1,24 @@
|
|||
{
|
||||
"extends": [
|
||||
"config:base"
|
||||
],
|
||||
"automerge": true,
|
||||
"major": {
|
||||
"automerge": false
|
||||
},
|
||||
"updateNotScheduled": false,
|
||||
"timezone": "America/New_York",
|
||||
"schedule": [
|
||||
"every weekend"
|
||||
],
|
||||
"lockFileMaintenance": {
|
||||
"enabled": true
|
||||
},
|
||||
"separatePatchReleases": true,
|
||||
"separateMultipleMajor": true,
|
||||
"masterIssue": true,
|
||||
"labels": [
|
||||
"type: dependencies",
|
||||
"renovate"
|
||||
]
|
||||
}
|
||||
{
|
||||
"extends": [
|
||||
"config:base"
|
||||
],
|
||||
"automerge": true,
|
||||
"major": {
|
||||
"automerge": false
|
||||
},
|
||||
"updateNotScheduled": false,
|
||||
"timezone": "America/New_York",
|
||||
"schedule": [
|
||||
"every weekend"
|
||||
],
|
||||
"lockFileMaintenance": {
|
||||
"enabled": true
|
||||
},
|
||||
"separatePatchReleases": true,
|
||||
"separateMultipleMajor": true,
|
||||
"masterIssue": true,
|
||||
"labels": [
|
||||
"type: dependencies",
|
||||
"renovate"
|
||||
]
|
||||
}
|
||||
|
|
20
src/index.js
20
src/index.js
|
@ -1,10 +1,10 @@
|
|||
'use strict'
|
||||
|
||||
// global cy, Cypress
|
||||
|
||||
const { functions } = require('./utils/index')
|
||||
|
||||
module.exports = {
|
||||
register: functions.register,
|
||||
tasks: functions.tasks
|
||||
}
|
||||
'use strict'
|
||||
|
||||
// global cy, Cypress
|
||||
|
||||
const { functions } = require('./utils/index')
|
||||
|
||||
module.exports = {
|
||||
register: functions.register,
|
||||
tasks: functions.tasks
|
||||
}
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
'use strict'
|
||||
|
||||
/* eslint-env mocha */
|
||||
const api = require('.')
|
||||
const la = require('lazy-ass')
|
||||
const is = require('check-more-types')
|
||||
|
||||
describe('@cypress/snapshot', () => {
|
||||
it('is an object', () => {
|
||||
la(is.object(api))
|
||||
})
|
||||
|
||||
it('has register', () => {
|
||||
la(is.fn(api.register))
|
||||
})
|
||||
})
|
||||
'use strict'
|
||||
|
||||
/* eslint-env mocha */
|
||||
const api = require('.')
|
||||
const la = require('lazy-ass')
|
||||
const is = require('check-more-types')
|
||||
|
||||
describe('@cypress/snapshot', () => {
|
||||
it('is an object', () => {
|
||||
la(is.object(api))
|
||||
})
|
||||
|
||||
it('has register', () => {
|
||||
la(is.fn(api.register))
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const readFileMaybe = require("../tasks/readFileMaybe");
|
||||
|
||||
module.exports = (on, config) => {
|
||||
on("task", { readFileMaybe });
|
||||
};
|
||||
const readFileMaybe = require("../tasks/readFileMaybe");
|
||||
|
||||
module.exports = (on, config) => {
|
||||
on("task", { readFileMaybe });
|
||||
};
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
const lazy = require("lazy-ass");
|
||||
const is = require("check-more-types");
|
||||
const snapshot = require("../snapshots/snapshot");
|
||||
|
||||
module.exports = () => {
|
||||
lazy(is.fn(global.before), "Missing global before function");
|
||||
lazy(is.fn(global.after), "Missing global after function");
|
||||
lazy(is.object(global.Cypress), "Missing Cypress object");
|
||||
|
||||
Cypress.Commands.add("snapshot", { prevSubject: true }, snapshot);
|
||||
|
||||
return snapshot;
|
||||
};
|
||||
const lazy = require("lazy-ass");
|
||||
const is = require("check-more-types");
|
||||
const snapshot = require("../snapshots/snapshot");
|
||||
|
||||
module.exports = () => {
|
||||
lazy(is.fn(global.before), "Missing global before function");
|
||||
lazy(is.fn(global.after), "Missing global after function");
|
||||
lazy(is.object(global.Cypress), "Missing Cypress object");
|
||||
|
||||
Cypress.Commands.add("snapshot", { prevSubject: true }, snapshot);
|
||||
|
||||
return snapshot;
|
||||
};
|
||||
|
|
|
@ -1,28 +1,28 @@
|
|||
const serializeToHTML = require("./serializers/serializeToHTML");
|
||||
const serializeDomElement = require("./serializers/serializeDomElement");
|
||||
const compareValues = require("./snapshots/compareValues");
|
||||
const readFileMaybe = require("./tasks/readFileMaybe");
|
||||
const identity = (x) => x;
|
||||
const publicProps = (name) => !name.startsWith("__");
|
||||
const countSnapshots = (snapshots) =>
|
||||
Object.keys(snapshots).filter(publicProps).length;
|
||||
|
||||
module.exports = {
|
||||
serializers: {
|
||||
serializeDomElement,
|
||||
serializeToHTML,
|
||||
identity,
|
||||
countSnapshots,
|
||||
},
|
||||
snapshots: {
|
||||
compareValues,
|
||||
},
|
||||
|
||||
functions: {
|
||||
register: require("./functions/register"),
|
||||
tasks: require("./functions/addTasks")
|
||||
},
|
||||
tasks: {
|
||||
readFileMaybe
|
||||
},
|
||||
};
|
||||
const serializeToHTML = require("./serializers/serializeToHTML");
|
||||
const serializeDomElement = require("./serializers/serializeDomElement");
|
||||
const compareValues = require("./snapshots/compareValues");
|
||||
const readFileMaybe = require("./tasks/readFileMaybe");
|
||||
const identity = (x) => x;
|
||||
const publicProps = (name) => !name.startsWith("__");
|
||||
const countSnapshots = (snapshots) =>
|
||||
Object.keys(snapshots).filter(publicProps).length;
|
||||
|
||||
module.exports = {
|
||||
serializers: {
|
||||
serializeDomElement,
|
||||
serializeToHTML,
|
||||
identity,
|
||||
countSnapshots,
|
||||
},
|
||||
snapshots: {
|
||||
compareValues,
|
||||
},
|
||||
|
||||
functions: {
|
||||
register: require("./functions/register"),
|
||||
tasks: require("./functions/addTasks")
|
||||
},
|
||||
tasks: {
|
||||
readFileMaybe
|
||||
},
|
||||
};
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
// remove React and Angular ids, which are transient
|
||||
module.exports = function deleteTransientIdsFromJson(json) {
|
||||
if (json.attributes) {
|
||||
delete json.attributes["data-reactid"];
|
||||
|
||||
Object.keys(json.attributes)
|
||||
.filter((key) => key.startsWith("_ng"))
|
||||
.forEach((attr) => delete json.attributes[attr]);
|
||||
delete json.attributes[""];
|
||||
}
|
||||
|
||||
if (Array.isArray(json.childNodes)) {
|
||||
json.childNodes.forEach(deleteTransientIdsFromJson);
|
||||
}
|
||||
return json;
|
||||
};
|
||||
// remove React and Angular ids, which are transient
|
||||
module.exports = function deleteTransientIdsFromJson(json) {
|
||||
if (json.attributes) {
|
||||
delete json.attributes["data-reactid"];
|
||||
|
||||
Object.keys(json.attributes)
|
||||
.filter((key) => key.startsWith("_ng"))
|
||||
.forEach((attr) => delete json.attributes[attr]);
|
||||
delete json.attributes[""];
|
||||
}
|
||||
|
||||
if (Array.isArray(json.childNodes)) {
|
||||
json.childNodes.forEach(deleteTransientIdsFromJson);
|
||||
}
|
||||
return json;
|
||||
};
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
const sd = require("@wildpeaks/snapshot-dom");
|
||||
const deleteTransientIdsFromJson = require("./deleteTransientIdsFromJson");
|
||||
|
||||
// converts DOM element to a JSON object
|
||||
module.exports = function serializeDomElement($el) {
|
||||
// console.log('snapshot value!', $el)
|
||||
const json = sd.toJSON($el[0]);
|
||||
// console.log('as json', json)
|
||||
|
||||
// hmm, why is value not serialized?
|
||||
if ($el.context.value && !json.attributes.value) {
|
||||
json.attributes.value = $el.context.value;
|
||||
}
|
||||
|
||||
return deleteTransientIdsFromJson(json);
|
||||
};
|
||||
const sd = require("@wildpeaks/snapshot-dom");
|
||||
const deleteTransientIdsFromJson = require("./deleteTransientIdsFromJson");
|
||||
|
||||
// converts DOM element to a JSON object
|
||||
module.exports = function serializeDomElement($el) {
|
||||
// console.log('snapshot value!', $el)
|
||||
const json = sd.toJSON($el[0]);
|
||||
// console.log('as json', json)
|
||||
|
||||
// hmm, why is value not serialized?
|
||||
if ($el.context.value && !json.attributes.value) {
|
||||
json.attributes.value = $el.context.value;
|
||||
}
|
||||
|
||||
return deleteTransientIdsFromJson(json);
|
||||
};
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
const stripTransientIdAttributes = require("./stripTransientIdAttributes");
|
||||
const beautify = require("js-beautify").html;
|
||||
|
||||
|
||||
module.exports = (el$) => {
|
||||
const html = el$[0].outerHTML;
|
||||
const stripped = stripTransientIdAttributes(html);
|
||||
const options = {
|
||||
wrap_line_length: 80,
|
||||
indent_inner_html: true,
|
||||
indent_size: 2,
|
||||
wrap_attributes: "force",
|
||||
};
|
||||
const pretty = beautify(stripped, options);
|
||||
return pretty;
|
||||
};
|
||||
const stripTransientIdAttributes = require("./stripTransientIdAttributes");
|
||||
const beautify = require("js-beautify").html;
|
||||
|
||||
|
||||
module.exports = (el$) => {
|
||||
const html = el$[0].outerHTML;
|
||||
const stripped = stripTransientIdAttributes(html);
|
||||
const options = {
|
||||
wrap_line_length: 80,
|
||||
indent_inner_html: true,
|
||||
indent_size: 2,
|
||||
wrap_attributes: "force",
|
||||
};
|
||||
const pretty = beautify(stripped, options);
|
||||
return pretty;
|
||||
};
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
module.exports = (html) => {
|
||||
const dataReactId = /data\-reactid="[\.\d\$\-abcdfef]+"/g;
|
||||
const angularId = /_ng(content|host)\-[0-9a-z-]+(="")?/g;
|
||||
return html.replace(dataReactId, "").replace(angularId, "");
|
||||
};
|
||||
module.exports = (html) => {
|
||||
const dataReactId = /data\-reactid="[\.\d\$\-abcdfef]+"/g;
|
||||
const angularId = /_ng(content|host)\-[0-9a-z-]+(="")?/g;
|
||||
return html.replace(dataReactId, "").replace(angularId, "");
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const compare = require("snap-shot-compare");
|
||||
module.exports = function compareValues({ expected, value }) {
|
||||
const noColor = false;
|
||||
const json = true;
|
||||
return compare({ expected, value, noColor, json });
|
||||
};
|
||||
const compare = require("snap-shot-compare");
|
||||
module.exports = function compareValues({ expected, value }) {
|
||||
const noColor = false;
|
||||
const json = true;
|
||||
return compare({ expected, value, noColor, json });
|
||||
};
|
||||
|
|
|
@ -1,130 +1,130 @@
|
|||
const serializeDomElement = require("../serializers/serializeDomElement");
|
||||
const serializeToHTML = require("../serializers/serializeToHTML");
|
||||
const compareValues = require("./compareValues");
|
||||
const { initStore } = require("snap-shot-store");
|
||||
const path = require("path");
|
||||
const identity = (x) => x;
|
||||
|
||||
const pickSerializer = (asJson, value) => {
|
||||
if (Cypress.dom.isJquery(value)) {
|
||||
return asJson ? serializeDomElement : serializeToHTML;
|
||||
}
|
||||
return identity;
|
||||
};
|
||||
|
||||
let counters = {};
|
||||
|
||||
const newStore = (name) => {
|
||||
return initStore(name);
|
||||
};
|
||||
|
||||
const get_snapshot_key = (key) => {
|
||||
if (key in counters) {
|
||||
// eslint-disable-next-line immutable/no-mutation
|
||||
counters[key] += 1;
|
||||
} else {
|
||||
// eslint-disable-next-line immutable/no-mutation
|
||||
counters[key] = 1;
|
||||
}
|
||||
return counters[key];
|
||||
};
|
||||
|
||||
const store_snapshot = (store, props = { value, name, path, raiser }) => {
|
||||
const fileName = props.name
|
||||
.join("_")
|
||||
.replace(/ /gi, "-")
|
||||
.replace(/\//gi, "-");
|
||||
const snapshotPath =
|
||||
props.path ||
|
||||
Cypress.config("snapshot").snapshotPath ||
|
||||
"cypress/snapshots";
|
||||
|
||||
const expectedPath = path.join(snapshotPath, `${fileName}.json`);
|
||||
cy.task("readFileMaybe", expectedPath).then((exist) => {
|
||||
if (exist) {
|
||||
props.raiser({ value: props.value, expected: JSON.parse(exist) });
|
||||
} else {
|
||||
cy.writeFile(expectedPath, JSON.stringify(props.value));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const set_snapshot = (
|
||||
store,
|
||||
{ snapshotName, snapshotPath, serialized, value }
|
||||
) => {
|
||||
if (!store) return;
|
||||
|
||||
const message = Cypress._.last(snapshotName);
|
||||
console.log("Current Snapshot name", snapshotName);
|
||||
|
||||
const devToolsLog = { $el: serialized };
|
||||
|
||||
if (Cypress.dom.isJquery(value)) {
|
||||
devToolsLog.$el = value;
|
||||
}
|
||||
|
||||
const options = {
|
||||
name: "snapshot",
|
||||
message,
|
||||
consoleProps: () => devToolsLog,
|
||||
};
|
||||
|
||||
if (value) options.$el = value;
|
||||
|
||||
const raiser = ({ value, expected }) => {
|
||||
const result = compareValues({ expected, value });
|
||||
result.orElse((json) => {
|
||||
devToolsLog.message = json.message;
|
||||
devToolsLog.expected = expected;
|
||||
delete devToolsLog.value;
|
||||
devToolsLog.value = value;
|
||||
|
||||
throw new Error(
|
||||
`Snapshot Difference. To update, delete snapshot file and rerun test.\n${json.message}`
|
||||
);
|
||||
});
|
||||
};
|
||||
Cypress.log(options);
|
||||
|
||||
store_snapshot(store, {
|
||||
value,
|
||||
name: snapshotName,
|
||||
path: snapshotPath,
|
||||
raiser,
|
||||
});
|
||||
};
|
||||
|
||||
const get_test_name = (test) => test.titlePath;
|
||||
const get_snapshot_name = (test, custom_name) => {
|
||||
const names = get_test_name(test);
|
||||
|
||||
const index = custom_name;
|
||||
names.push(String(index));
|
||||
|
||||
if (custom_name) return [custom_name];
|
||||
return names;
|
||||
};
|
||||
|
||||
module.exports = (value, step, options) => {
|
||||
if (typeof step === "object") options = step;
|
||||
if (typeof value !== "object" || Array.isArray(value))
|
||||
value = { data: value };
|
||||
console.log("value", value);
|
||||
|
||||
const name = get_snapshot_name(
|
||||
Cypress.currentTest,
|
||||
options.snapshotName || step
|
||||
);
|
||||
const serializer = pickSerializer(options.json, value);
|
||||
const serialized = serializer(value);
|
||||
const store = newStore(serialized || {});
|
||||
|
||||
console.log({ step, options });
|
||||
set_snapshot(store, {
|
||||
snapshotName: name,
|
||||
snapshotPath: options.snapshotPath,
|
||||
serialized,
|
||||
value,
|
||||
});
|
||||
};
|
||||
const serializeDomElement = require("../serializers/serializeDomElement");
|
||||
const serializeToHTML = require("../serializers/serializeToHTML");
|
||||
const compareValues = require("./compareValues");
|
||||
const { initStore } = require("snap-shot-store");
|
||||
const path = require("path");
|
||||
const identity = (x) => x;
|
||||
|
||||
const pickSerializer = (asJson, value) => {
|
||||
if (Cypress.dom.isJquery(value)) {
|
||||
return asJson ? serializeDomElement : serializeToHTML;
|
||||
}
|
||||
return identity;
|
||||
};
|
||||
|
||||
let counters = {};
|
||||
|
||||
const newStore = (name) => {
|
||||
return initStore(name);
|
||||
};
|
||||
|
||||
const get_snapshot_key = (key) => {
|
||||
if (key in counters) {
|
||||
// eslint-disable-next-line immutable/no-mutation
|
||||
counters[key] += 1;
|
||||
} else {
|
||||
// eslint-disable-next-line immutable/no-mutation
|
||||
counters[key] = 1;
|
||||
}
|
||||
return counters[key];
|
||||
};
|
||||
|
||||
const store_snapshot = (store, props = { value, name, path, raiser }) => {
|
||||
const fileName = props.name
|
||||
.join("_")
|
||||
.replace(/ /gi, "-")
|
||||
.replace(/\//gi, "-");
|
||||
const snapshotPath =
|
||||
props.path ||
|
||||
Cypress.config("snapshot").snapshotPath ||
|
||||
"cypress/snapshots";
|
||||
|
||||
const expectedPath = path.join(snapshotPath, `${fileName}.json`);
|
||||
cy.task("readFileMaybe", expectedPath).then((exist) => {
|
||||
if (exist) {
|
||||
props.raiser({ value: props.value, expected: JSON.parse(exist) });
|
||||
} else {
|
||||
cy.writeFile(expectedPath, JSON.stringify(props.value));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const set_snapshot = (
|
||||
store,
|
||||
{ snapshotName, snapshotPath, serialized, value }
|
||||
) => {
|
||||
if (!store) return;
|
||||
|
||||
const message = Cypress._.last(snapshotName);
|
||||
console.log("Current Snapshot name", snapshotName);
|
||||
|
||||
const devToolsLog = { $el: serialized };
|
||||
|
||||
if (Cypress.dom.isJquery(value)) {
|
||||
devToolsLog.$el = value;
|
||||
}
|
||||
|
||||
const options = {
|
||||
name: "snapshot",
|
||||
message,
|
||||
consoleProps: () => devToolsLog,
|
||||
};
|
||||
|
||||
if (value) options.$el = value;
|
||||
|
||||
const raiser = ({ value, expected }) => {
|
||||
const result = compareValues({ expected, value });
|
||||
result.orElse((json) => {
|
||||
devToolsLog.message = json.message;
|
||||
devToolsLog.expected = expected;
|
||||
delete devToolsLog.value;
|
||||
devToolsLog.value = value;
|
||||
|
||||
throw new Error(
|
||||
`Snapshot Difference. To update, delete snapshot file and rerun test.\n${json.message}`
|
||||
);
|
||||
});
|
||||
};
|
||||
Cypress.log(options);
|
||||
|
||||
store_snapshot(store, {
|
||||
value,
|
||||
name: snapshotName,
|
||||
path: snapshotPath,
|
||||
raiser,
|
||||
});
|
||||
};
|
||||
|
||||
const get_test_name = (test) => test.titlePath;
|
||||
const get_snapshot_name = (test, custom_name) => {
|
||||
const names = get_test_name(test);
|
||||
|
||||
const index = custom_name;
|
||||
names.push(String(index));
|
||||
|
||||
if (custom_name) return [custom_name];
|
||||
return names;
|
||||
};
|
||||
|
||||
module.exports = (value, step, options) => {
|
||||
if (typeof step === "object") options = step;
|
||||
if (typeof value !== "object" || Array.isArray(value))
|
||||
value = { data: value };
|
||||
console.log("value", value);
|
||||
|
||||
const name = get_snapshot_name(
|
||||
Cypress.currentTest,
|
||||
options.snapshotName || step
|
||||
);
|
||||
const serializer = pickSerializer(options.json, value);
|
||||
const serialized = serializer(value);
|
||||
const store = newStore(serialized || {});
|
||||
|
||||
console.log({ step, options });
|
||||
set_snapshot(store, {
|
||||
snapshotName: name,
|
||||
snapshotPath: options.snapshotPath,
|
||||
serialized,
|
||||
value,
|
||||
});
|
||||
};
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
const fs = require("fs");
|
||||
|
||||
module.exports = (filename) => {
|
||||
if (fs.existsSync(filename)) {
|
||||
return fs.readFileSync(filename, "utf8");
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
const fs = require("fs");
|
||||
|
||||
module.exports = (filename) => {
|
||||
if (fs.existsSync(filename)) {
|
||||
return fs.readFileSync(filename, "utf8");
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue