Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit f2f7d76

Browse files
stuartlongbahmutov
authored andcommitted
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
1 parent a68d97b commit f2f7d76

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('focused input field', () => {
4848
})
4949
```
5050

51-
The snapshot object can be found in file `snapshots.js`. In the above case it would look something like this
51+
By default, the snapshot object can be found in file `snapshots.js`. In the above case it would look something like this
5252

5353
```js
5454
module.exports = {
@@ -103,6 +103,23 @@ cy.get(...).snapshot({
103103
})
104104
```
105105

106+
### Configuration
107+
108+
This module provides some configuration options:
109+
110+
#### useRelativeSnapshots
111+
Set to true in order to store your snapshots for each test run next to the inital test caller rather
112+
than at the base working directory.
113+
114+
**Note:** requires the `readFileMaybe` plugin to be configured see https://on.cypress.io/task#Read-a-file-that-might-not-exist
115+
116+
#### snapshotFileName
117+
Set to a string to name your snapshot something other than 'snapshots.js'
118+
119+
#### Usage
120+
Set the configuration options as part of the Cypress config.
121+
See https://docs.cypress.io/guides/references/configuration.html
122+
106123
## Debugging
107124

108125
To debug this module run with environment variable `DEBUG=@cypress/snapshot`

src/index.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { initStore } = require('snap-shot-store')
66
const la = require('lazy-ass')
77
const is = require('check-more-types')
88
const compare = require('snap-shot-compare')
9+
const path = require('path')
910

1011
const {
1112
serializeDomElement,
@@ -14,6 +15,14 @@ const {
1415
countSnapshots
1516
} = require('./utils')
1617

18+
const DEFAULT_CONFIG_OPTIONS = {
19+
// using relative snapshots requires a simple
20+
// 'readFileMaybe' plugin to be configured
21+
// see https://on.cypress.io/task#Read-a-file-that-might-not-exist
22+
useRelativeSnapshots: false,
23+
snapshotFileName: 'snapshots.js'
24+
}
25+
1726
/* eslint-disable no-console */
1827

1928
function compareValues ({ expected, value }) {
@@ -27,6 +36,12 @@ function registerCypressSnapshot () {
2736
la(is.fn(global.after), 'missing global after function')
2837
la(is.object(global.Cypress), 'missing Cypress object')
2938

39+
const useRelative = Cypress.config("useRelativeSnapshots")
40+
const config = {
41+
useRelativeSnapshots: useRelative === undefined ? DEFAULT_CONFIG_OPTIONS.useRelativeSnapshots : useRelative,
42+
snapshotFileName: Cypress.config("snapshotFileName") || DEFAULT_CONFIG_OPTIONS.snapshotFileName
43+
}
44+
3045
console.log('registering @cypress/snapshot')
3146

3247
let storeSnapshot
@@ -48,7 +63,15 @@ function registerCypressSnapshot () {
4863
return counters[key]
4964
}
5065

51-
const SNAPSHOT_FILENAME = 'snapshots.js'
66+
let snapshotFileName = config.snapshotFileName
67+
if (config.useRelativeSnapshots) {
68+
let relative = Cypress.spec.relative
69+
if (Cypress.platform === 'win32') {
70+
relative = relative.replace(/\\/g, path.sep)
71+
}
72+
73+
snapshotFileName = path.join(path.dirname(relative), config.snapshotFileName)
74+
}
5275

5376
function evaluateLoadedSnapShots (js) {
5477
la(is.string(js), 'expected JavaScript snapshot source', js)
@@ -59,9 +82,24 @@ function registerCypressSnapshot () {
5982
}
6083

6184
global.before(function loadSnapshots () {
62-
cy
63-
.readFile(SNAPSHOT_FILENAME, 'utf-8', { log: false })
64-
.then(evaluateLoadedSnapShots)
85+
let readFile
86+
87+
if (config.useRelativeSnapshots) {
88+
readFile = cy
89+
.task('readFileMaybe', snapshotFileName)
90+
.then(function (contents) {
91+
if (!contents) {
92+
return cy.writeFile(snapshotFileName, '', 'utf-8', { log: false })
93+
}
94+
95+
return contents
96+
})
97+
} else {
98+
readFile = cy
99+
.readFile(snapshotFileName, 'utf-8')
100+
}
101+
102+
readFile.then(evaluateLoadedSnapShots)
65103
// no way to catch an error yet
66104
})
67105

@@ -161,7 +199,7 @@ function registerCypressSnapshot () {
161199
snapshots.__version = Cypress.version
162200
const s = JSON.stringify(snapshots, null, 2)
163201
const str = `module.exports = ${s}\n`
164-
cy.writeFile(SNAPSHOT_FILENAME, str, 'utf-8', { log: false })
202+
cy.writeFile(snapshotFileName, str, 'utf-8', { log: false })
165203
}
166204
})
167205

0 commit comments

Comments
 (0)