-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathinitCypressTests.js
64 lines (50 loc) · 2.4 KB
/
initCypressTests.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
// This file is merged in a <script type=module> into index.html
// it will be used to load and kick start the selected spec
const CypressInstance = window.Cypress = parent.Cypress
const importsToLoad = []
/* Support file import logic, this should be removed once we
* are able to return relative paths from the supportFile
* Jira #UNIFY-1260
*/
const supportFile = CypressInstance.config('supportFile')
const projectRoot = CypressInstance.config('projectRoot')
const devServerPublicPathRoute = CypressInstance.config('devServerPublicPathRoute')
let supportRelativeToProjectRoot = supportFile.replace(projectRoot, '')
if (CypressInstance.config('platform') === 'win32') {
supportRelativeToProjectRoot = supportFile.replace(projectRoot.replaceAll('/', '\\'))
}
if (supportFile) {
// We need a slash before /cypress/supportFile.js, this happens by default
// with the current string replacement logic.
importsToLoad.push(() => import(`${devServerPublicPathRoute}${supportRelativeToProjectRoot}`))
}
/* Spec file import logic */
// We need a slash before /src/my-spec.js, this does not happen by default.
importsToLoad.push(() => import(`${devServerPublicPathRoute}/${CypressInstance.spec.relative}`))
if (!CypressInstance) {
throw new Error('Tests cannot run without a reference to Cypress!')
}
// load the support and spec
CypressInstance.onSpecWindow(window, importsToLoad)
// then start the test process
CypressInstance.action('app:window:before:load', window)
// Before all tests we are mounting the root element,
// Cleaning up platform between tests is the responsibility of the specific adapter
// because unmounting react/vue component should be done using specific framework API
// (for devtools and to get rid of global event listeners from previous tests.)
CypressInstance.on('test:before:run', () => {
// leave the error overlay alone if it exists
if (document.body.querySelectorAll('vite-error-overlay').length) {
// make the error more readable by giving it more space
Cypress.action('cy:viewport:changed', { viewportWidth: 1000, viewportHeight: 500 })
return
}
// reset the viewport to default when in normal mode
Cypress.action('cy:viewport:changed', {
viewportWidth: Cypress.config('viewportWidth'),
viewportHeight: Cypress.config('viewportHeight'),
})
})
// Make usage of node test plugins possible
window.global = window
window.process = typeof process !== 'undefined' ? process : {}