-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
cypress.config.js
150 lines (122 loc) · 4.62 KB
/
cypress.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const { defineConfig } = require('cypress')
/* eslint-disable no-console */
const AdmZip = require('adm-zip')
const globby = require('globby')
const { stripIndent } = require('common-tags')
const { rmdir } = require('fs')
const { readExcelFile } = require('./document-utils/read-excel')
const { readPdf } = require('./document-utils/read-pdf')
module.exports = defineConfig({
fixturesFolder: false,
viewportWidth: 500,
viewportHeight: 900,
chromeWebSecurity: false,
pageLoadTimeout: 5000,
e2e: {
baseUrl: 'http://localhost:8070',
supportFile: false,
specPattern: 'cypress/e2e/**/*.cy.{js,jsx,ts,tsx}',
setupNodeEvents (on, config) {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
// register utility tasks to read and parse Excel files
on('task', {
readExcelFile,
readPdf,
validateZipFile (filename) {
// now let's validate the downloaded ZIP file
// the validations depend on your projects. For this example
// we will check that the zip has two entries "a.txt" and "b.txt"
// and that the contents of the zipped file "a.txt" are the same as expected
// Tip: use https://github.com/cthackers/adm-zip to load and unzip the Zip contents
console.log('loading zip', filename)
const zip = new AdmZip(filename)
const zipEntries = zip.getEntries()
const names = zipEntries.map((entry) => entry.entryName).sort()
console.log('zip file %s has entries %o', filename, names)
// since this code is running in a Node process we do not have built-in "expect" or "assert" functions
// instead we can throw an Error object which fails the "cy.task" command
if (names.length !== 2) {
throw new Error(
`List of files ${names.join(',')} in ${filename} has extra files`
)
}
// if there is no error, let's print positive message to the terminal
// to let the user know this validation was successful
// console.log('✅ number of entries')
if (!names.includes('a.txt')) {
throw new Error(
`List of files ${names.join(',')} in ${filename} is missing a.txt`
)
}
console.log('✅ has a.txt entry')
if (!names.includes('b.txt')) {
throw new Error(
`List of files ${names.join(',')} in ${filename} is missing b.txt`
)
}
console.log('✅ has b.txt entry')
// confirm the contents of an entry inside the Zip file
// the entry is just a text file in our case
// let's grab its text content and compare to the expected string
const aEntry = zip.readAsText('a.txt').trim()
const expectedText = stripIndent`
hello zip
and Cypress recipes
`
if (aEntry !== expectedText) {
console.error('Expected file a.txt to have text')
console.error('------')
console.error(expectedText)
console.error('------')
console.error('but it had text')
console.error('------')
console.error(aEntry)
console.error('------')
throw new Error(stripIndent`
Invalid a.txt entry in the zip file ${filename}
See terminal for more details
`)
}
console.log('✅ a.txt file has the expected contents')
// any other validations?
return null
},
// a task to find one file matching the given mask
// returns just the first matching file
async findFiles (mask) {
if (!mask) {
throw new Error('Missing a file mask to search')
}
console.log('searching for files %s', mask)
const list = await globby(mask)
if (!list.length) {
console.log('found no files')
return null
}
console.log('found %d files, first one %s', list.length, list[0])
return list[0]
},
deleteFolder (folderName) {
console.log('deleting folder %s', folderName)
return new Promise((resolve, reject) => {
rmdir(
folderName,
{
maxRetries: 10,
recursive: true,
},
(err) => {
if (err) {
console.error(err)
return reject(err)
}
resolve(null)
}
)
})
},
})
},
},
})