forked from cypress-io/cypress
/
reporter.errors.cy.ts
356 lines (316 loc) · 10.4 KB
/
reporter.errors.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import * as specLoader from './support/spec-loader'
import { createVerify, verifyInternalFailure } from './support/verify-failures'
type VerifyFunc = (specTitle: string, verifyOptions: any) => void
/**
* Navigates to desired error spec file within Cypress app and waits for completion.
* Returns scoped verify function to aid inner spec validation.
*/
function loadErrorSpec (options: specLoader.LoadSpecOptions): VerifyFunc {
const {
filePath,
hasPreferredIde = false,
mode,
} = options
specLoader.loadSpec(options)
// Return scoped verify function with spec options baked in
return createVerify({ fileName: Cypress._.last(filePath.split('/')), hasPreferredIde, mode })
}
describe('errors ui', {
viewportHeight: 768,
viewportWidth: 1024,
// Limiting tests kept in memory due to large memory cost
// of nested spec snapshots
numTestsKeptInMemory: 0,
}, () => {
it('exception failures', () => {
const verify = loadErrorSpec({
filePath: 'errors/exceptions.cy.js',
failCount: 2,
})
verify('in spec file', {
column: 10,
message: 'bar is not a function',
})
verify('in file outside project', {
message: 'An outside error',
stackRegex: /\/throws\-error\.js:5:8/,
codeFrameRegex: /\/throws\-error\.js:5:9/,
codeFrameText: `thrownewError('An outside error')`,
})
})
it('hooks', { viewportHeight: 900 }, () => {
const verify = loadErrorSpec({
filePath: 'errors/hooks.cy.js',
failCount: 1,
})
// https://github.com/cypress-io/cypress/issues/8214
// https://github.com/cypress-io/cypress/issues/8288
// https://github.com/cypress-io/cypress/issues/8350
verify('test', {
column: [7, 18], // [chrome, firefox]
codeFrameText: 'beforeEach(()=>',
message: `Cypress detected you registered a(n) beforeEach hook while a test was running`,
})
})
it('uncaught errors', () => {
const verify = loadErrorSpec({
filePath: 'errors/uncaught.cy.js',
failCount: 11,
})
verify('sync app visit exception', {
uncaught: true,
command: 'visit',
originalMessage: 'visit error',
message: [
'The following error originated from your application code',
],
notInMessage: [
'It was caused by an unhandled promise rejection',
],
regex: /localhost\:\d+\/cypress\/fixtures\/errors.html\?error-on-visit:\d+:\d+/,
hasCodeFrame: false,
})
verify('sync app navigates to visit exception', {
uncaught: true,
originalMessage: 'visit error',
message: [
'The following error originated from your application code',
],
notInMessage: [
'It was caused by an unhandled promise rejection',
],
regex: /localhost\:\d+\/cypress\/fixtures\/errors.html\?error-on-visit:\d+:\d+/,
hasCodeFrame: false,
})
verify('sync app exception', {
uncaught: true,
command: 'click',
originalMessage: 'sync error',
message: [
'The following error originated from your application code',
],
notInMessage: [
'It was caused by an unhandled promise rejection',
],
regex: /localhost\:\d+\/cypress\/fixtures\/errors.html:\d+:\d+/,
hasCodeFrame: false,
})
verify('exception inside uncaught:exception', {
uncaught: true,
uncaughtMessage: 'sync error',
column: 12,
originalMessage: 'bar is not a function',
message: [
'The following error originated from your test code',
],
notInMessage: [
'It was caused by an unhandled promise rejection',
],
})
verify('async app exception', {
uncaught: true,
originalMessage: 'async error',
message: [
'The following error originated from your application code',
],
notInMessage: [
'It was caused by an unhandled promise rejection',
],
regex: /localhost\:\d+\/cypress\/fixtures\/errors.html:\d+:\d+/,
hasCodeFrame: false,
})
verify('app unhandled rejection', {
uncaught: true,
originalMessage: 'promise rejection',
message: [
'The following error originated from your application code',
'It was caused by an unhandled promise rejection',
],
regex: /localhost\:\d+\/cypress\/fixtures\/errors.html:\d+:\d+/,
hasCodeFrame: false,
})
verify('async spec exception', {
uncaught: true,
column: 12,
originalMessage: 'bar is not a function',
message: [
'The following error originated from your test code',
],
notInMessage: [
'It was caused by an unhandled promise rejection',
],
})
verify('async spec exception with done', {
uncaught: true,
column: 12,
originalMessage: 'bar is not a function',
message: [
'The following error originated from your test code',
],
notInMessage: [
'It was caused by an unhandled promise rejection',
],
})
verify('spec unhandled rejection', {
uncaught: true,
column: 20,
originalMessage: 'Unhandled promise rejection from the spec',
message: [
'The following error originated from your test code',
'It was caused by an unhandled promise rejection',
],
})
verify('spec unhandled rejection with string content', {
uncaught: true,
column: 20,
originalMessage: 'Unhandled promise rejection with string content from the spec',
message: [
'The following error originated from your test code',
'It was caused by an unhandled promise rejection',
],
stackRegex: /.*/,
hasCodeFrame: false,
})
verify('spec unhandled rejection with done', {
uncaught: true,
column: 20,
originalMessage: 'Unhandled promise rejection from the spec',
message: [
'The following error originated from your test code',
'It was caused by an unhandled promise rejection',
],
})
verify('spec Bluebird unhandled rejection', {
uncaught: true,
column: 21,
originalMessage: 'Unhandled promise rejection from the spec',
message: [
'The following error originated from your test code',
'It was caused by an unhandled promise rejection',
],
})
verify('spec Bluebird unhandled rejection with done', {
uncaught: true,
column: 21,
originalMessage: 'Unhandled promise rejection from the spec',
message: [
'The following error originated from your test code',
'It was caused by an unhandled promise rejection',
],
})
})
it('uncaught errors: outside test', () => {
const verify = loadErrorSpec({
filePath: 'errors/uncaught_outside_test.cy.js',
failCount: 1,
})
// NOTE: the following 2 test don't have uncaught: true because we don't
// display command logs if there are only events and not true commands
// and uncaught: true causes the verification to look for the error
// event command log
verify('An uncaught error was detected outside of a test', {
column: 7,
message: [
'The following error originated from your test code',
'error from outside test',
'Cypress could not associate this error to any specific test',
],
codeFrameText: `thrownewError('error from outside test')`,
})
})
it('uncaught errors: outside test only suite', () => {
const verify = loadErrorSpec({
filePath: 'errors/uncaught_outside_test_only_suite.cy.js',
failCount: 1,
})
verify('An uncaught error was detected outside of a test', {
column: 7,
message: [
'error from outside test with only suite',
'The following error originated from your test code',
'Cypress could not associate this error to any specific test',
],
codeFrameText: `thrownewError('error from outside test with only suite')`,
})
})
it('typescript', () => {
const verify = loadErrorSpec({
filePath: 'errors/typescript.cy.ts',
failCount: 3,
})
verify('assertion failure', {
column: 25,
message: `expected 'actual' to equal 'expected'`,
})
verify('exception', {
column: 10,
message: 'bar is not a function',
})
verify('command failure', {
column: 8,
message: 'Timed out retrying after 0ms: Expected to find element: #does-not-exist, but never found it',
codeFrameText: `.get('#does-not-exist')`,
})
})
context('docs url', () => {
before(() => {
// docs_url.cy.js manually sets the 'isInteractive' config property in order
// to test both run and open mode behavior. We need to skip the config validation
// here in order for this to be possible.
// @ts-ignore
window.top.__cySkipValidateConfig = true
})
after(() => {
// @ts-ignore
window.top.__cySkipValidateConfig = false
})
it('docs url validation', { retries: 1 }, () => {
const docsUrl = 'https://on.cypress.io/viewport'
const verify = loadErrorSpec({
filePath: 'errors/docs_url.cy.js',
failCount: 2,
})
verify('displays as link in interactive mode', {
verifyFn () {
cy.contains('.runnable-title', 'displays as link in interactive mode')
.closest('.runnable').within(() => {
cy
.get('.runnable-err-message')
.should('not.contain', docsUrl)
.contains('Learn more')
.should('have.attr', 'href', docsUrl)
})
},
})
verify('is text in error message in run mode', {
verifyFn () {
cy.contains('.runnable-title', 'is text in error message in run mode')
.closest('.runnable').within(() => {
cy
.get('.runnable-err-message')
.should('contain', docsUrl)
.contains('Learn more')
.should('not.exist')
})
},
})
})
})
// cases where there is a bug in Cypress and we should show cypress internals
// instead of the invocation stack. we test this by monkey-patching internal
// methods to make them throw an error
it('unexpected errors', () => {
const verify = loadErrorSpec({
filePath: 'errors/unexpected.cy.js',
failCount: 2,
})
verify('Cypress method error', {
verifyFn: verifyInternalFailure,
method: 'Cypress.LocalStorage.clear',
})
verify('internal cy error', {
verifyFn: verifyInternalFailure,
method: 'cy.expect',
})
})
})