Skip to content

Commit acc61d8

Browse files
feat: add currentRetry to Cypress API (#25297)
Co-authored-by: Emily Rohrbough <[email protected]> Co-authored-by: Emily Rohrbough <[email protected]>
1 parent 736c599 commit acc61d8

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

cli/types/cypress.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,11 @@ declare namespace Cypress {
420420
titlePath: string[]
421421
}
422422

423+
/**
424+
* Information about current test retry
425+
*/
426+
currentRetry: number
427+
423428
/**
424429
* Information about the browser currently running the tests
425430
*/

packages/driver/cypress/e2e/cypress/cypress.cy.js

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,96 @@ describe('driver/src/cypress/index', () => {
8686
})
8787
})
8888

89+
context('.currentRetry', () => {
90+
describe('test is not retried', () => {
91+
before(() => {
92+
expect(Cypress.currentRetry).to.eq(0)
93+
})
94+
95+
beforeEach(() => {
96+
expect(Cypress.currentRetry).to.eq(0)
97+
})
98+
99+
afterEach(() => {
100+
expect(Cypress.currentRetry).to.eq(0)
101+
})
102+
103+
after(() => {
104+
expect(Cypress.currentRetry).to.eq(0)
105+
})
106+
107+
it('correctly returns currentRetry', () => {
108+
expect(Cypress.currentRetry).to.eq(0)
109+
})
110+
})
111+
112+
describe('test is retried due to beforeEach hook failure', { retries: 1 }, () => {
113+
before(() => {
114+
expect(Cypress.currentRetry).to.be.oneOf([0, 1])
115+
})
116+
117+
beforeEach(() => {
118+
expect(Cypress.currentRetry).to.eq(1)
119+
})
120+
121+
it('correctly returns currentRetry', () => {
122+
expect(Cypress.currentRetry).to.eq(1)
123+
})
124+
125+
afterEach(() => {
126+
expect(Cypress.currentRetry).to.eq(1)
127+
})
128+
129+
after(() => {
130+
expect(Cypress.currentRetry).to.eq(1)
131+
})
132+
})
133+
134+
describe('test is retried due to test failure', { retries: 1 }, () => {
135+
before(() => {
136+
expect(Cypress.currentRetry).to.be.oneOf([0, 1])
137+
})
138+
139+
beforeEach(() => {
140+
expect(Cypress.currentRetry).to.be.oneOf([0, 1])
141+
})
142+
143+
it('correctly returns currentRetry', () => {
144+
expect(Cypress.currentRetry).to.eq(1)
145+
})
146+
147+
afterEach(() => {
148+
expect(Cypress.currentRetry).to.eq(1)
149+
})
150+
151+
after(() => {
152+
expect(Cypress.currentRetry).to.eq(1)
153+
})
154+
})
155+
156+
describe('test is retried due to afterEach hook failure', { retries: 1 }, () => {
157+
before(() => {
158+
expect(Cypress.currentRetry).to.be.oneOf([0, 1])
159+
})
160+
161+
beforeEach(() => {
162+
expect(Cypress.currentRetry).to.be.oneOf([0, 1])
163+
})
164+
165+
it('correctly returns currentRetry', () => {
166+
expect(Cypress.currentRetry).to.be.oneOf([0, 1])
167+
})
168+
169+
afterEach(() => {
170+
expect(Cypress.currentRetry).to.eq(1)
171+
})
172+
173+
after(() => {
174+
expect(Cypress.currentRetry).to.eq(1)
175+
})
176+
})
177+
})
178+
89179
context('.isCy', () => {
90180
it('returns true on cy, cy chainable', () => {
91181
expect(Cypress.isCy(cy)).to.be.true
@@ -104,7 +194,7 @@ describe('driver/src/cypress/index', () => {
104194
})
105195
})
106196

107-
context('.Log', () => {
197+
context('.log', () => {
108198
it('throws when passing non-object to Cypress.log()', () => {
109199
const fn = () => {
110200
Cypress.log('My Log')

packages/driver/src/cypress.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,12 @@ class $Cypress {
787787
}
788788
}
789789

790+
get currentRetry (): number {
791+
const ctx = this.cy.state('runnable').ctx
792+
793+
return ctx?.currentTest?._currentRetry || ctx?.test?._currentRetry
794+
}
795+
790796
static create (config: Record<string, any>) {
791797
const cypress = new $Cypress()
792798

0 commit comments

Comments
 (0)