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

Commit 6601322

Browse files
committed
Uninvoked empty snapshot fix
Updated evaluateLoadedSnapShots() in index.js to store newly generated snapshots. Also, global.before() checks on the snapshots content initially to prevent the creation of an empty snapshot file.
1 parent 0a6277c commit 6601322

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"no-debugger": "warn"
88
},
99
"env": {
10+
"mocha":true,
1011
"node": true
1112
}
1213
}

src/index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ function registerCypressSnapshot () {
7474
}
7575

7676
function evaluateLoadedSnapShots (js) {
77-
la(is.string(js), 'expected JavaScript snapshot source', js)
78-
console.log('read snapshots.js file')
79-
const store = eval(js) || {}
77+
let store = {}
78+
if (js != null) {
79+
la(is.string(js), 'expected JavaScript snapshot source', js)
80+
console.log('read snapshots.js file')
81+
store = eval(js) || {}
82+
console.log('have %d snapshot(s)', countSnapshots(store))
83+
}
8084
console.log('have %d snapshot(s)', countSnapshots(store))
8185
storeSnapshot = initStore(store)
8286
}
@@ -87,13 +91,6 @@ function registerCypressSnapshot () {
8791
if (config.useRelativeSnapshots) {
8892
readFile = cy
8993
.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-
})
9794
} else {
9895
readFile = cy
9996
.readFile(snapshotFileName, 'utf-8')
@@ -193,13 +190,16 @@ function registerCypressSnapshot () {
193190
global.after(function saveSnapshots () {
194191
if (storeSnapshot) {
195192
const snapshots = storeSnapshot()
196-
console.log('%d snapshot(s) on finish', countSnapshots(snapshots))
193+
const count = countSnapshots(snapshots)
194+
console.log('%d snapshot(s) on finish', count)
197195
console.log(snapshots)
198196

199-
snapshots.__version = Cypress.version
200-
const s = JSON.stringify(snapshots, null, 2)
201-
const str = `module.exports = ${s}\n`
202-
cy.writeFile(snapshotFileName, str, 'utf-8', { log: false })
197+
if (count) {
198+
snapshots.__version = Cypress.version
199+
const s = JSON.stringify(snapshots, null, 2)
200+
const str = `module.exports = ${s}\n`
201+
cy.writeFile(snapshotFileName, str, 'utf-8', { log: false })
202+
}
203203
}
204204
})
205205

0 commit comments

Comments
 (0)