@@ -6,6 +6,7 @@ const { initStore } = require('snap-shot-store')
6
6
const la = require ( 'lazy-ass' )
7
7
const is = require ( 'check-more-types' )
8
8
const compare = require ( 'snap-shot-compare' )
9
+ const path = require ( 'path' )
9
10
10
11
const {
11
12
serializeDomElement,
@@ -14,6 +15,14 @@ const {
14
15
countSnapshots
15
16
} = require ( './utils' )
16
17
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
+
17
26
/* eslint-disable no-console */
18
27
19
28
function compareValues ( { expected, value } ) {
@@ -27,6 +36,12 @@ function registerCypressSnapshot () {
27
36
la ( is . fn ( global . after ) , 'missing global after function' )
28
37
la ( is . object ( global . Cypress ) , 'missing Cypress object' )
29
38
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
+
30
45
console . log ( 'registering @cypress/snapshot' )
31
46
32
47
let storeSnapshot
@@ -48,7 +63,15 @@ function registerCypressSnapshot () {
48
63
return counters [ key ]
49
64
}
50
65
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
+ }
52
75
53
76
function evaluateLoadedSnapShots ( js ) {
54
77
la ( is . string ( js ) , 'expected JavaScript snapshot source' , js )
@@ -59,9 +82,24 @@ function registerCypressSnapshot () {
59
82
}
60
83
61
84
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 )
65
103
// no way to catch an error yet
66
104
} )
67
105
@@ -161,7 +199,7 @@ function registerCypressSnapshot () {
161
199
snapshots . __version = Cypress . version
162
200
const s = JSON . stringify ( snapshots , null , 2 )
163
201
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 } )
165
203
}
166
204
} )
167
205
0 commit comments