Skip to content

Commit 9f9b665

Browse files
chore: update win.cookie calls to win.document.cookie call in test (#31527)
1 parent ac3bb18 commit 9f9b665

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

packages/driver/cypress/e2e/commands/sessions/sessions.cy.ts

+44-16
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ describe('cy.session', { retries: 0 }, () => {
5959
.then(async () => {
6060
cy.spy(Cypress, 'action').log(false)
6161

62-
await Cypress.action('runner:test:before:after:run:async', {}, Cypress.state('runnable'), { nextTestHasTestIsolationOn: true })
62+
await Cypress.action('runner:test:before:after:run:async', {}, Cypress.state('runnable'), {
63+
nextTestHasTestIsolationOn: true,
64+
})
6365

6466
expect(Cypress.action).to.be.calledWith('cy:url:changed', '')
6567
expect(Cypress.action).to.be.calledWith('cy:visit:blank', { testIsolation: true })
@@ -73,7 +75,9 @@ describe('cy.session', { retries: 0 }, () => {
7375
.then(async () => {
7476
cy.spy(Cypress, 'action').log(false)
7577

76-
await Cypress.action('runner:test:before:after:run:async', {}, Cypress.state('runnable'), { nextTestHasTestIsolationOn: undefined })
78+
await Cypress.action('runner:test:before:after:run:async', {}, Cypress.state('runnable'), {
79+
nextTestHasTestIsolationOn: undefined,
80+
})
7781

7882
expect(Cypress.action).to.be.calledWith('cy:url:changed', '')
7983
expect(Cypress.action).to.be.calledWith('cy:visit:blank', { testIsolation: true })
@@ -87,7 +91,9 @@ describe('cy.session', { retries: 0 }, () => {
8791
.then(async () => {
8892
cy.spy(Cypress, 'action').log(false)
8993

90-
await Cypress.action('runner:test:before:after:run:async', {}, Cypress.state('runnable'), { nextTestHasTestIsolationOn: false })
94+
await Cypress.action('runner:test:before:after:run:async', {}, Cypress.state('runnable'), {
95+
nextTestHasTestIsolationOn: false,
96+
})
9197

9298
expect(Cypress.action).not.to.be.calledWith('cy:url:changed', '')
9399
expect(Cypress.action).not.to.be.calledWith('cy:visit:blank', { testIsolation: true })
@@ -99,31 +105,52 @@ describe('cy.session', { retries: 0 }, () => {
99105
it('clears the browser cookie after each run', () => {
100106
cy.window()
101107
.then((win) => {
102-
// @ts-expect-error TODO: cookie doesn't exist on win, usually cookie is set on document
103-
win.cookie = 'key=value; SameSite=Strict; Secure; Path=/fixtures'
108+
// Check that the only cookie present is the Cypress initial cookie
109+
expect(win.document.cookie).to.satisfy((cookie) => {
110+
return !cookie || cookie === '__cypress.initial=true'
111+
})
112+
113+
win.document.cookie = 'key=value; SameSite=Strict; Secure; Path=/fixtures'
104114
})
105115
.then(async () => {
106116
cy.spy(Cypress, 'action').log(false)
107117

108-
await Cypress.action('runner:test:before:after:run:async', {}, Cypress.state('runnable'), { nextTestHasTestIsolationOn: true })
118+
await Cypress.action('runner:test:before:after:run:async', {}, Cypress.state('runnable'), {
119+
nextTestHasTestIsolationOn: true,
120+
})
109121
})
110122

111-
cy.window().its('cookie').should('be.undefined')
123+
cy.window().then((win) => {
124+
// Check that the only cookie present is the Cypress initial cookie
125+
expect(win.document.cookie).to.satisfy((cookie) => {
126+
return !cookie || cookie === '__cypress.initial=true'
127+
})
128+
})
112129
})
113130

114131
it('does not clear the browser cookie after each run if the next test has test isolation off', () => {
115132
cy.window()
116133
.then((win) => {
117-
// @ts-expect-error TODO: cookie doesn't exist on win, usually cookie is set on document
118-
win.cookie = 'key=value; SameSite=Strict; Secure; Path=/fixtures'
134+
// Check that the only cookie present is the Cypress initial cookie
135+
expect(win.document.cookie).to.satisfy((cookie) => {
136+
return !cookie || cookie === '__cypress.initial=true'
137+
})
138+
139+
win.document.cookie = 'key=value'
140+
expect(win.document.cookie).to.include('key=value')
119141
})
120142
.then(async () => {
121143
cy.spy(Cypress, 'action').log(false)
122144

123-
await Cypress.action('runner:test:before:after:run:async', {}, Cypress.state('runnable'), { nextTestHasTestIsolationOn: false })
145+
await Cypress.action('runner:test:before:after:run:async', {}, Cypress.state('runnable'), {
146+
nextTestHasTestIsolationOn: false,
147+
})
124148
})
125149

126-
cy.window().its('cookie').should('eq', 'key=value; SameSite=Strict; Secure; Path=/fixtures')
150+
cy.window()
151+
.then((win) => {
152+
expect(win.document.cookie).to.include('key=value')
153+
})
127154
})
128155
})
129156

@@ -896,8 +923,7 @@ describe('cy.session', { retries: 0 }, () => {
896923
it('does not clear the browser context before each run', () => {
897924
cy.window()
898925
.then((win) => {
899-
// @ts-expect-error TODO: cookie doesn't exist on win, usually cookie is set on document
900-
win.cookie = 'key=value; SameSite=Strict; Secure; Path=/fixtures'
926+
win.document.cookie = 'key=value; SameSite=Strict; Secure; Path=/fixtures'
901927
win.localStorage.setItem('animal', 'bear')
902928
win.sessionStorage.setItem('food', 'burgers')
903929
})
@@ -913,9 +939,11 @@ describe('cy.session', { retries: 0 }, () => {
913939
expect(Cypress.action).not.to.be.calledWith('cy:visit:blank')
914940
})
915941

916-
cy.window().its('cookie').should('equal', 'key=value; SameSite=Strict; Secure; Path=/fixtures')
917-
cy.window().its('localStorage').should('have.length', 1).should('deep.contain', { animal: 'bear' })
918-
cy.window().its('sessionStorage').should('have.length', 1).should('deep.contain', { food: 'burgers' })
942+
cy.window().then((win) => {
943+
expect(win.document.cookie).to.contain('key=value')
944+
expect(win.localStorage).to.have.length(1).and.to.deep.contain({ animal: 'bear' })
945+
expect(win.sessionStorage).to.have.length(1).and.to.deep.contain({ food: 'burgers' })
946+
})
919947
})
920948
})
921949

0 commit comments

Comments
 (0)