-
Notifications
You must be signed in to change notification settings - Fork 54
/
cypress.e2e.config.ts
130 lines (112 loc) · 5.07 KB
/
cypress.e2e.config.ts
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
import { defineConfig } from 'cypress'
import { createClient } from 'redis'
import webpackPreprocessor from '@cypress/webpack-preprocessor'
import { PNG } from 'pngjs'
import pixelmatch from 'pixelmatch'
import fs from 'fs'
import path from 'path'
import { createEntry } from './webpack.config'
const downloadDirectory = path.join(__dirname, '..', 'downloads')
const checkFileDownloaded = async (filename: string, timeout: number, delayMs = 10): Promise<string | undefined> => {
const start = Date.now()
const fullFileName = `${downloadDirectory}/${filename}`
while (Date.now() - start < timeout) {
await new Promise((res) => setTimeout(res, delayMs))
if (fs.existsSync(fullFileName)) {
return fullFileName
}
}
}
export default defineConfig({
video: false,
defaultCommandTimeout: 20000,
requestTimeout: 8000,
pageLoadTimeout: 80000,
projectId: 'twojfp',
viewportWidth: 1200,
viewportHeight: 1080,
trashAssetsBeforeRuns: true,
// cypress default is 'top' this means sometimes the element is underneath the top navbar
// not what a human would do... so, set it to center to avoid this weird behavior
scrollBehavior: 'center',
retries: { runMode: 2 },
e2e: {
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
config.env.E2E_TESTING = !!process.env.E2E_TESTING
const options = {
webpackOptions: createEntry('cypress'),
watchOptions: {},
}
// @ts-expect-error -- ignore errors in options type
on('file:preprocessor', webpackPreprocessor(options))
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('cypress-terminal-report/src/installLogsPrinter')(on)
} catch (e) {}
on('before:browser:launch', (browser, launchOptions) => {
if (browser.name === 'chrome') {
// https://www.ghacks.net/2013/10/06/list-useful-google-chrome-command-line-switches/
// Compatibility with gh actions
launchOptions.args.push('--window-size=1280,720')
return launchOptions
}
})
on('task', {
compareToReferenceImage({ source, reference, diffThreshold = 0.01, ms = 10000 }) {
return checkFileDownloaded(source, ms).then((fileExists) => {
if (!fileExists) {
return null
}
const imgSrc = PNG.sync.read(fs.readFileSync(`${downloadDirectory}/${source}`))
const imgRef = PNG.sync.read(fs.readFileSync(path.join(__dirname, reference)))
const { width, height } = imgSrc
const imgDiff = new PNG({ width, height })
const numDiffPixels = pixelmatch(imgSrc.data, imgRef.data, imgDiff.data, width, height, {
threshold: 0.1,
})
const imgDiffFilename = `${downloadDirectory}/${source}.diff.png`
fs.writeFileSync(imgDiffFilename, PNG.sync.write(imgDiff))
const percentageDiff = numDiffPixels / (width * height)
if (percentageDiff > diffThreshold) {
throw new Error(
`Reference image is off by ${(percentageDiff * 100).toFixed(
2
)}% (${numDiffPixels}) pixels. See ${imgDiffFilename} for more info`
)
}
return true
})
},
async resetInsightCache() {
const redisClient = await createClient()
.on('error', (err) => console.log('Redis client error', err))
.connect()
// Clear cache
for await (const key of redisClient.scanIterator({
TYPE: 'string',
MATCH: '*cache*',
COUNT: 500,
})) {
await redisClient.del(key)
}
// Also clear the more ephemeral async query statuses
for await (const key of redisClient.scanIterator({
TYPE: 'string',
MATCH: 'query_async*',
COUNT: 500,
})) {
await redisClient.del(key)
}
await redisClient.quit()
return null // Cypress requires _some_ return value
},
})
return config
},
baseUrl: 'http://localhost:8000',
specPattern: 'cypress/e2e/**/*.{js,jsx,ts,tsx}',
chromeWebSecurity: false,
},
})