mirror of
https://github.com/datashard/snapshot.git
synced 2024-11-21 21:52:28 +00:00
feat: add support for relative snapshots (#21)
* feat: Add capability for relative snapshot files * feat: remove path from package * feat: remove nsp from pre-push since it's deprecated * feat: use Cypress config instead * feat: fix build
This commit is contained in:
parent
9670a9e90a
commit
66de7256fc
2 changed files with 61 additions and 6 deletions
19
README.md
19
README.md
|
@ -48,7 +48,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 +103,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`
|
||||
|
|
48
src/index.js
48
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
|
||||
})
|
||||
|
||||
|
@ -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…
Reference in a new issue