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:
Stuart Long 2019-04-26 13:38:33 -07:00 committed by Gleb Bahmutov
parent 9670a9e90a
commit 66de7256fc
2 changed files with 61 additions and 6 deletions

View file

@ -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 ```js
module.exports = { 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 ## Debugging
To debug this module run with environment variable `DEBUG=@cypress/snapshot` To debug this module run with environment variable `DEBUG=@cypress/snapshot`

View file

@ -6,6 +6,7 @@ const { initStore } = require('snap-shot-store')
const la = require('lazy-ass') const la = require('lazy-ass')
const is = require('check-more-types') const is = require('check-more-types')
const compare = require('snap-shot-compare') const compare = require('snap-shot-compare')
const path = require('path')
const { const {
serializeDomElement, serializeDomElement,
@ -14,6 +15,14 @@ const {
countSnapshots countSnapshots
} = require('./utils') } = 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 */ /* eslint-disable no-console */
function compareValues ({ expected, value }) { function compareValues ({ expected, value }) {
@ -27,6 +36,12 @@ function registerCypressSnapshot () {
la(is.fn(global.after), 'missing global after function') la(is.fn(global.after), 'missing global after function')
la(is.object(global.Cypress), 'missing Cypress object') 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') console.log('registering @cypress/snapshot')
let storeSnapshot let storeSnapshot
@ -48,7 +63,15 @@ function registerCypressSnapshot () {
return counters[key] 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) { function evaluateLoadedSnapShots (js) {
la(is.string(js), 'expected JavaScript snapshot source', js) la(is.string(js), 'expected JavaScript snapshot source', js)
@ -59,9 +82,24 @@ function registerCypressSnapshot () {
} }
global.before(function loadSnapshots () { global.before(function loadSnapshots () {
cy let readFile
.readFile(SNAPSHOT_FILENAME, 'utf-8', { log: false })
.then(evaluateLoadedSnapShots) 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 // no way to catch an error yet
}) })
@ -161,7 +199,7 @@ function registerCypressSnapshot () {
snapshots.__version = Cypress.version snapshots.__version = Cypress.version
const s = JSON.stringify(snapshots, null, 2) const s = JSON.stringify(snapshots, null, 2)
const str = `module.exports = ${s}\n` const str = `module.exports = ${s}\n`
cy.writeFile(SNAPSHOT_FILENAME, str, 'utf-8', { log: false }) cy.writeFile(snapshotFileName, str, 'utf-8', { log: false })
} }
}) })