From f2f7d76d756f31d02c1eb697966ce759e306a55c Mon Sep 17 00:00:00 2001 From: Stuart Long Date: Fri, 26 Apr 2019 13:38:33 -0700 Subject: [PATCH] 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 --- README.md | 19 ++++++++++++++++++- src/index.js | 48 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a20d841..df6b088 100644 --- a/README.md +++ b/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` diff --git a/src/index.js b/src/index.js index 4144779..1ad4b70 100644 --- a/src/index.js +++ b/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 }) } })