-
Notifications
You must be signed in to change notification settings - Fork 26
/
cypress.ci-config.js
111 lines (99 loc) · 3.25 KB
/
cypress.ci-config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const fs = require('fs')
const path = require('path')
const debug = require('debug')('cypress-workshop-basics')
const { defineConfig } = require('cypress')
module.exports = defineConfig({
viewportWidth: 600,
viewportHeight: 800,
e2e: {
baseUrl: 'http://localhost:3000',
env: {},
specPattern: 'cypress/ci-tests/*-spec.js',
setupNodeEvents(on, config) {
// `on` is used to hook into various events Cypress emits
// "cy.task" can be used from specs to "jump" into Node environment
// and doing anything you might want. For example, checking "data.json" file!
// https://on.cypress.io/task
on('task', {
// saves given or default empty data object into todomvc/data.json file
// if the server is watching this file, next reload should show the updated values
async resetData(dataToSet = DEFAULT_DATA) {
resetData(dataToSet)
// add a small delay for the server to "notice"
// the changed JSON file and reload
await delay(100)
// cy.task handlers should always return something
// otherwise it might be an accidental return
return null
},
hasSavedRecord(title, ms = 3000) {
debug('inside task')
console.log(
'looking for title "%s" in the database (time limit %dms)',
title,
ms
)
return hasRecordAsync(title, ms)
},
/**
* Call this method using cy.task('getSavedTodos') command.
* Make sure the backend had plenty of time to save the data.
*/
getSavedTodos() {
const s = fs.readFileSync(getDbFilename(), 'utf8')
const data = JSON.parse(s)
console.log('returning %d saved todos', data.todos.length)
return data.todos
}
})
on('before:spec', (spec) => {
console.log('resetting DB before spec %s', spec.name)
resetData()
})
}
},
projectId: '89mmxs'
})
const getDbFilename = () => path.join(__dirname, 'todomvc', 'data.json')
const findRecord = (title) => {
const dbFilename = getDbFilename()
const contents = JSON.parse(fs.readFileSync(dbFilename))
const todos = contents.todos
return todos.find((record) => record.title === title)
}
const hasRecordAsync = (title, ms) => {
const delay = 50
return new Promise((resolve, reject) => {
if (ms < 0) {
return reject(new Error(`Could not find record with title "${title}"`))
}
const found = findRecord(title)
if (found) {
return resolve(found)
}
setTimeout(() => {
hasRecordAsync(title, ms - delay).then(resolve, reject)
}, 50)
})
}
/**
* Default object representing our "database" file in "todomvc/data.json"
*/
const DEFAULT_DATA = {
todos: []
}
const resetData = (dataToSet = DEFAULT_DATA) => {
const dbFilename = getDbFilename()
debug('reset data file %s with %o', dbFilename, dataToSet)
if (!dataToSet) {
console.error('Cannot save empty object in %s', dbFilename)
throw new Error('Cannot save empty object in resetData')
}
const str = JSON.stringify(dataToSet, null, 2) + '\n'
fs.writeFileSync(dbFilename, str, 'utf8')
}
async function delay(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}