-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathunhandled-promise-in-test.cy.js
More file actions
60 lines (52 loc) · 2.01 KB
/
unhandled-promise-in-test.cy.js
File metadata and controls
60 lines (52 loc) · 2.01 KB
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
/// <reference types="cypress" />
/* eslint-env browser */
/* eslint-disable no-console */
describe('Unhandled promises in the test code', () => {
// NOTE: this test will pass in Cypress < 7.0 and fail in Cypress 7.0+
it.skip('does not affect the Cypress test', () => {
Cypress.Promise.delay(1000).then(() => {
throw new Error('Test code has a rejected promise')
})
cy.visit('index.html')
// because the promise will be rejected after one second
// wait inside the test
cy.wait(1100)
})
// NOTE: the test fails because we catch the unhandled promise rejection
it.skip('fails test on unhandled rejection in the test code that uses Cypress.Promise', () => {
// Cypress promises are Bluebird promises
// https://on.cypress.io/promise
// and have a callback for unhandled rejections
// it will catch any rejected promises created using Cypress.Promise
Cypress.Promise.onPossiblyUnhandledRejection((error, promise) => {
throw error
})
Cypress.Promise.delay(1000).then(() => {
throw new Error('Test code has a rejected promise')
})
cy.visit('index.html')
// because the promise will be rejected after one second
// wait inside the test
cy.wait(1100)
})
// NOTE: the test fails because we catch the unhandled promise rejection
it.skip('fails test on unhandled rejection in the test code that uses built-in Promise', () => {
// note: since we are registering this event listener
// on the spec's window and not on the application's window
// Cypress does NOT reset it after every test
window.addEventListener('unhandledrejection', (event) => {
throw event.reason
})
// use the built-in browser promises,
// reject it after one second
new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('Rejected native promise'))
}, 1000)
})
cy.visit('index.html')
// because the promise will be rejected after one second
// wait inside the test
cy.wait(1100)
})
})