mirror of
https://github.com/datashard/snapshot.git
synced 2025-05-16 04:07:21 +00:00
Compare commits
41 commits
Author | SHA1 | Date | |
---|---|---|---|
|
0a14849823 | ||
|
28462fc9e9 | ||
|
bdad90afd5 | ||
|
0a6277cee8 | ||
|
9a34a0b643 | ||
|
8d5e82d1b6 | ||
|
d0b1084f98 | ||
|
0b5a37487a | ||
|
bc08a1d270 | ||
|
cc5c9a9fd4 | ||
|
c1a21de66d | ||
|
1bc9013b0c | ||
|
bd560d6af9 | ||
|
7c8658d0ef | ||
|
ac9a2afb49 | ||
|
40f062389c | ||
|
fb4d9fe89a | ||
|
c836e5531e | ||
|
9fdc5ec16f | ||
|
89000de57f | ||
|
4c35f7e2bf | ||
|
ad1ed62a71 | ||
|
4481828479 | ||
|
140f94d1b0 | ||
|
9e863c08a4 | ||
|
c8e3ee89c1 | ||
|
f927fb13fe | ||
|
41f76f29e5 | ||
|
c7e36ef39b | ||
|
a743cfbccc | ||
|
7ac7e096a7 | ||
|
7e3141f6dc | ||
|
ce7a6569b9 | ||
|
6340cc2f6f | ||
|
34c503323b | ||
|
cc47c50650 | ||
|
f2f7d76d75 | ||
|
a68d97bb76 | ||
|
ac3afd73b2 | ||
|
21941f89c7 | ||
|
a7f44204b3 |
4 changed files with 7111 additions and 3393 deletions
23
README.md
23
README.md
|
@ -8,6 +8,10 @@
|
|||
[![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 6 or above.
|
||||
|
@ -48,7 +52,7 @@ describe('focused input field', () => {
|
|||
})
|
||||
```
|
||||
|
||||
The snapshot object can be found in file `snapshots.js`. In the above case it would look something like this
|
||||
By default, the snapshot object can be found in file `snapshots.js`. In the above case it would look something like this
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
|
@ -103,6 +107,23 @@ cy.get(...).snapshot({
|
|||
})
|
||||
```
|
||||
|
||||
### Configuration
|
||||
|
||||
This module provides some configuration options:
|
||||
|
||||
#### useRelativeSnapshots
|
||||
Set to true in order to store your snapshots for each test run next to the inital test caller rather
|
||||
than at the base working directory.
|
||||
|
||||
**Note:** requires the `readFileMaybe` plugin to be configured see https://on.cypress.io/task#Read-a-file-that-might-not-exist
|
||||
|
||||
#### snapshotFileName
|
||||
Set to a string to name your snapshot something other than 'snapshots.js'
|
||||
|
||||
#### Usage
|
||||
Set the configuration options as part of the Cypress config.
|
||||
See https://docs.cypress.io/guides/references/configuration.html
|
||||
|
||||
## Debugging
|
||||
|
||||
To debug this module run with environment variable `DEBUG=@cypress/snapshot`
|
||||
|
|
10385
package-lock.json
generated
10385
package-lock.json
generated
File diff suppressed because it is too large
Load diff
46
package.json
46
package.json
|
@ -4,26 +4,6 @@
|
|||
"version": "0.0.0-development",
|
||||
"author": "Gleb Bahmutov <gleb@cypress.io>",
|
||||
"bugs": "https://github.com/cypress-io/snapshot/issues",
|
||||
"config": {
|
||||
"pre-git": {
|
||||
"commit-msg": "simple",
|
||||
"pre-commit": [
|
||||
"npm prune",
|
||||
"npm run deps",
|
||||
"npm test",
|
||||
"git add src/*.js",
|
||||
"npm run ban"
|
||||
],
|
||||
"pre-push": [
|
||||
"npm run unused-deps",
|
||||
"npm run license",
|
||||
"npm run ban -- --all",
|
||||
"npm run size"
|
||||
],
|
||||
"post-commit": [],
|
||||
"post-merge": []
|
||||
}
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
},
|
||||
|
@ -62,37 +42,31 @@
|
|||
"unit": "mocha src/*-spec.js",
|
||||
"unused-deps": "dependency-check --unused --no-dev . --entry src/add-initial-snapshot-file.js",
|
||||
"postinstall": "node src/add-initial-snapshot-file.js",
|
||||
"semantic-release": "semantic-release pre && npm publish --access public && semantic-release post",
|
||||
"semantic-release": "semantic-release",
|
||||
"cypress:open": "cypress open",
|
||||
"cypress:run": "cypress run"
|
||||
},
|
||||
"release": {
|
||||
"analyzeCommits": "simple-commit-message",
|
||||
"generateNotes": "github-post-release"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ban-sensitive-files": "1.9.2",
|
||||
"cypress": "1.1.4",
|
||||
"debug": "3.1.0",
|
||||
"dependency-check": "2.9.1",
|
||||
"deps-ok": "1.2.1",
|
||||
"eslint": "4.13.0",
|
||||
"cypress": "3.8.0",
|
||||
"debug": "3.2.6",
|
||||
"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",
|
||||
"github-post-release": "1.13.1",
|
||||
"license-checker": "15.0.0",
|
||||
"mocha": "4.0.1",
|
||||
"pre-git": "3.16.0",
|
||||
"semantic-release": "^8.2.0"
|
||||
"mocha": "6.1.4",
|
||||
"semantic-release": "15.13.32"
|
||||
},
|
||||
"dependencies": {
|
||||
"@wildpeaks/snapshot-dom": "1.2.1",
|
||||
"am-i-a-dependency": "1.1.2",
|
||||
"check-more-types": "2.24.0",
|
||||
"its-name": "1.0.0",
|
||||
"js-beautify": "1.7.5",
|
||||
"js-beautify": "1.10.2",
|
||||
"lazy-ass": "1.6.0",
|
||||
"snap-shot-compare": "2.7.1",
|
||||
"snap-shot-compare": "2.8.3",
|
||||
"snap-shot-store": "1.2.3"
|
||||
}
|
||||
}
|
||||
|
|
50
src/index.js
50
src/index.js
|
@ -6,6 +6,7 @@ const { initStore } = require('snap-shot-store')
|
|||
const la = require('lazy-ass')
|
||||
const is = require('check-more-types')
|
||||
const compare = require('snap-shot-compare')
|
||||
const path = require('path')
|
||||
|
||||
const {
|
||||
serializeDomElement,
|
||||
|
@ -14,6 +15,14 @@ const {
|
|||
countSnapshots
|
||||
} = require('./utils')
|
||||
|
||||
const DEFAULT_CONFIG_OPTIONS = {
|
||||
// using relative snapshots requires a simple
|
||||
// 'readFileMaybe' plugin to be configured
|
||||
// see https://on.cypress.io/task#Read-a-file-that-might-not-exist
|
||||
useRelativeSnapshots: false,
|
||||
snapshotFileName: 'snapshots.js'
|
||||
}
|
||||
|
||||
/* eslint-disable no-console */
|
||||
|
||||
function compareValues ({ expected, value }) {
|
||||
|
@ -27,6 +36,12 @@ function registerCypressSnapshot () {
|
|||
la(is.fn(global.after), 'missing global after function')
|
||||
la(is.object(global.Cypress), 'missing Cypress object')
|
||||
|
||||
const useRelative = Cypress.config('useRelativeSnapshots')
|
||||
const config = {
|
||||
useRelativeSnapshots: useRelative === undefined ? DEFAULT_CONFIG_OPTIONS.useRelativeSnapshots : useRelative,
|
||||
snapshotFileName: Cypress.config('snapshotFileName') || DEFAULT_CONFIG_OPTIONS.snapshotFileName
|
||||
}
|
||||
|
||||
console.log('registering @cypress/snapshot')
|
||||
|
||||
let storeSnapshot
|
||||
|
@ -48,7 +63,15 @@ function registerCypressSnapshot () {
|
|||
return counters[key]
|
||||
}
|
||||
|
||||
const SNAPSHOT_FILENAME = 'snapshots.js'
|
||||
let snapshotFileName = config.snapshotFileName
|
||||
if (config.useRelativeSnapshots) {
|
||||
let relative = Cypress.spec.relative
|
||||
if (Cypress.platform === 'win32') {
|
||||
relative = relative.replace(/\\/g, path.sep)
|
||||
}
|
||||
|
||||
snapshotFileName = path.join(path.dirname(relative), config.snapshotFileName)
|
||||
}
|
||||
|
||||
function evaluateLoadedSnapShots (js) {
|
||||
la(is.string(js), 'expected JavaScript snapshot source', js)
|
||||
|
@ -59,9 +82,24 @@ function registerCypressSnapshot () {
|
|||
}
|
||||
|
||||
global.before(function loadSnapshots () {
|
||||
cy
|
||||
.readFile(SNAPSHOT_FILENAME, 'utf-8', { log: false })
|
||||
.then(evaluateLoadedSnapShots)
|
||||
let readFile
|
||||
|
||||
if (config.useRelativeSnapshots) {
|
||||
readFile = cy
|
||||
.task('readFileMaybe', snapshotFileName)
|
||||
.then(function (contents) {
|
||||
if (!contents) {
|
||||
return cy.writeFile(snapshotFileName, '', 'utf-8', { log: false })
|
||||
}
|
||||
|
||||
return contents
|
||||
})
|
||||
} else {
|
||||
readFile = cy
|
||||
.readFile(snapshotFileName, 'utf-8')
|
||||
}
|
||||
|
||||
readFile.then(evaluateLoadedSnapShots)
|
||||
// no way to catch an error yet
|
||||
})
|
||||
|
||||
|
@ -120,7 +158,7 @@ function registerCypressSnapshot () {
|
|||
devToolsLog.expected = expected
|
||||
delete devToolsLog.value
|
||||
devToolsLog.value = value
|
||||
throw new Error(`Snapshot difference\n${json.message}`)
|
||||
throw new Error(`Snapshot difference. To update, delete snapshot and rerun test.\n${json.message}`)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -161,7 +199,7 @@ function registerCypressSnapshot () {
|
|||
snapshots.__version = Cypress.version
|
||||
const s = JSON.stringify(snapshots, null, 2)
|
||||
const str = `module.exports = ${s}\n`
|
||||
cy.writeFile(SNAPSHOT_FILENAME, str, 'utf-8', { log: false })
|
||||
cy.writeFile(snapshotFileName, str, 'utf-8', { log: false })
|
||||
}
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue