forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact.cy.ts
149 lines (122 loc) · 4.81 KB
/
react.cy.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/// <reference path="../support/e2e.ts" />
import type { fixtureDirs } from '@tooling/system-tests'
import dedent from 'dedent'
type ProjectDirs = typeof fixtureDirs
const WEBPACK_REACT: ProjectDirs[number][] = ['webpack4_wds3-react', 'webpack4_wds4-react', 'webpack5_wds3-react', 'webpack5_wds4-react']
// Add to this list to focus on a particular permutation
const ONLY_PROJECTS: ProjectDirs[number][] = []
for (const project of WEBPACK_REACT) {
if (ONLY_PROJECTS.length && !ONLY_PROJECTS.includes(project)) {
continue
}
describe(`Working with ${project}`, () => {
beforeEach(() => {
cy.scaffoldProject(project)
cy.openProject(project, ['--config-file', 'cypress-webpack.config.ts'])
cy.startAppServer('component')
})
it('should mount a passing test', () => {
cy.visitApp()
cy.contains('App.cy.jsx').click()
cy.waitForSpecToFinish()
cy.get('.passed > .num').should('contain', 1)
})
it('MissingReact: should fail, rerun, succeed', () => {
cy.on('uncaught:exception', () => {
// Ignore the uncaught exception in the AUT
return false
})
cy.visitApp()
cy.contains('MissingReact.cy.jsx').click()
cy.waitForSpecToFinish()
cy.get('.failed > .num').should('contain', 1)
cy.withCtx(async (ctx) => {
await ctx.actions.file.writeFileInProject(`src/MissingReact.jsx`,
`import React from 'react';
${await ctx.file.readFileInProject('src/MissingReact.jsx')}`)
})
cy.get('.passed > .num').should('contain', 1)
})
it('MissingReactInSpec: should fail, rerun, succeed', () => {
cy.on('uncaught:exception', () => {
// Ignore the uncaught exception in the AUT
return false
})
cy.visitApp()
cy.contains('MissingReactInSpec.cy.jsx').click()
cy.waitForSpecToFinish()
cy.get('.failed > .num').should('contain', 1)
cy.withCtx(async (ctx) => {
await ctx.actions.file.writeFileInProject(`src/MissingReactInSpec.cy.jsx`,
await ctx.file.readFileInProject('src/App.cy.jsx'))
})
cy.get('.passed > .num').should('contain', 1)
})
it('AppCompilationError: should fail with uncaught exception error', () => {
cy.on('uncaught:exception', () => {
// Ignore the uncaught exception in the AUT
return false
})
cy.visitApp()
cy.contains('AppCompilationError.cy.jsx').click()
cy.waitForSpecToFinish()
cy.get('.failed > .num').should('contain', 1)
cy.contains('An uncaught error was detected outside of a test')
cy.contains('The following error originated from your test code, not from Cypress.')
// Correct the problem
cy.withCtx(async (ctx) => {
await ctx.actions.file.writeFileInProject(
`src/AppCompilationError.cy.jsx`,
await ctx.file.readFileInProject('src/App.cy.jsx'),
)
})
cy.waitForSpecToFinish()
cy.get('.passed > .num').should('contain', 1)
const appCompilationErrorSpec = dedent`
import React from 'react'
import { mount } from 'cypress/react'
import { App } from './App'
it('renders hello world', () => {
mount(<App />)
cy.get('h1').contains('Hello World')
}
})
`
// Cause the problem again
cy.withCtx(async (ctx, o) => {
await ctx.actions.file.writeFileInProject(
`src/AppCompilationError.cy.jsx`,
o.appCompilationErrorSpec,
)
}, { appCompilationErrorSpec })
cy.waitForSpecToFinish()
cy.get('.failed > .num').should('contain', 1)
cy.contains('An uncaught error was detected outside of a test')
cy.contains('The following error originated from your test code, not from Cypress.')
})
// https://cypress-io.atlassian.net/browse/UNIFY-1697
it('filters missing spec files from loader during pre-compilation', () => {
cy.visitApp()
// 1. assert spec executes successfully
cy.contains('App.cy.jsx').click()
cy.get('.passed > .num').should('contain', 1)
// 2. remove file from file system
cy.withCtx(async (ctx) => {
await ctx.actions.file.removeFileInProject(`src/App.cy.jsx`)
})
// 3. assert redirect back to #/specs with alert presented
cy.contains('[data-cy="alert"]', 'Spec not found')
// 4. recreate spec, with same name as removed spec
cy.findByTestId('new-spec-button').click()
cy.findByRole('dialog').within(() => {
cy.get('input').clear().type('src/App.cy.jsx')
cy.contains('button', 'Create Spec').click()
})
cy.findByRole('dialog').within(() => {
cy.contains('button', 'Okay, run the spec').click()
})
// 5. assert recreated spec executes successfully
cy.get('.passed > .num').should('contain', 1)
})
})
}