Skip to content

Commit f1122fc

Browse files
authoredAug 12, 2022
fix: clear session state when changing specs in open mode (#23146)
1 parent 0729a68 commit f1122fc

File tree

16 files changed

+106
-62
lines changed

16 files changed

+106
-62
lines changed
 

‎npm/eslint-plugin-dev/test/no-return-before.spec.js

-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ describe(ruleName, () => {
9090
})
9191

9292
expect(result.errorCount).toBe(1)
93-
// console.log(result.messages[0].message)
9493

9594
expect(result.messages[0].message).toContain('someFn')
9695

‎npm/eslint-plugin-dev/test/skip-comment.spec.js

-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ describe('skip-comment', () => {
5454
})
5555

5656
expect(result.errorCount).toBe(3)
57-
// console.log(result.messages[0].message)
5857

5958
expect(result.messages[0].message).toContain('it')
6059
expect(result.messages[0].message).toContain('NOTE:')
@@ -82,7 +81,6 @@ describe('skip-comment', () => {
8281
})
8382

8483
expect(result.errorCount).toBe(1)
85-
// console.log(result.messages[0].message)
8684

8785
expect(result.messages[0].message).toContain('it')
8886
expect(result.messages[0].message).toContain('FOOBAR:')

‎npm/webpack-preprocessor/test/e2e/helpers.js

-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ exports.runTest = async (options = {}) => {
111111
})
112112
}
113113

114-
// console.log(stdout)
115114
console.log(`${chalk.bold('run matched these results:')} ${JSON.stringify(opts.expectedResults, null, 2)}`)
116115
})
117116
}

‎packages/app/cypress/e2e/runner/sessions.ui.cy.ts

+62-2
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ describe('runner/cypress sessions.ui.spec', {
4545
passCount: 1,
4646
})
4747

48-
validateSessionsInstrumentPanel(['blank_session'])
48+
validateSessionsInstrumentPanel(['user1'])
4949

5050
cy.get('.command-name-session')
5151
.within(() => {
5252
cy.get('.command-expander').first().click()
53-
cy.contains('blank_session')
53+
cy.contains('user1')
5454
cy.contains('created')
5555

5656
validateSetupSessionGroup()
@@ -309,3 +309,63 @@ describe('runner/cypress sessions.ui.spec', {
309309
cy.percySnapshot()
310310
})
311311
})
312+
313+
describe('runner/cypress sessions.open_mode.spec', () => {
314+
beforeEach(() => {
315+
cy.scaffoldProject('session-and-origin-e2e-specs')
316+
cy.openProject('session-and-origin-e2e-specs')
317+
cy.startAppServer('e2e')
318+
cy.visitApp()
319+
320+
cy.get('[data-cy-row="multiple_sessions.cy.js"]').click()
321+
cy.waitForSpecToFinish({
322+
passCount: 1,
323+
})
324+
325+
cy.get('.command-name-session').should('contain', 'user1')
326+
.find('.reporter-tag').should('contain', 'created')
327+
328+
cy.get('.command-name-session').should('contain', 'user2')
329+
.find('.reporter-tag').should('contain', 'created')
330+
})
331+
332+
it('persists spec sessions when clicking "rerun all tests" button', () => {
333+
cy.get('.restart').click()
334+
335+
cy.waitForSpecToFinish({
336+
passCount: 1,
337+
})
338+
339+
cy.get('.command-name-session').should('contain', 'user1')
340+
.find('.reporter-tag').should('contain', 'restored')
341+
342+
cy.get('.command-name-session').should('contain', 'user2')
343+
.find('.reporter-tag').should('contain', 'restored')
344+
})
345+
346+
it('persists spec sessions on refresh', () => {
347+
cy.get('body').type('r')
348+
349+
cy.waitForSpecToFinish({
350+
passCount: 1,
351+
})
352+
353+
cy.get('.command-name-session').should('contain', 'user1')
354+
.find('.reporter-tag').should('contain', 'restored')
355+
356+
cy.get('.command-name-session').should('contain', 'user2')
357+
.find('.reporter-tag').should('contain', 'restored')
358+
})
359+
360+
it('does not persists spec sessions when selecting a different spec', () => {
361+
cy.get('body').type('f')
362+
cy.get('div[title="new_session.cy.js"]').click()
363+
364+
cy.waitForSpecToFinish({
365+
passCount: 1,
366+
})
367+
368+
cy.get('.command-name-session').should('contain', 'user1')
369+
.find('.reporter-tag').should('contain', 'created')
370+
})
371+
})

‎packages/app/cypress/e2e/support/execute-spec.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { shouldHaveTestResults } from '../runner/support/spec-loader'
2+
13
declare global {
24
namespace Cypress {
35
interface Chainable {
@@ -9,15 +11,19 @@ declare global {
911
* 3. Waits (with a timeout of 30s) for the Rerun all tests button to be present. This ensures all tests have completed
1012
*
1113
*/
12-
waitForSpecToFinish(): void
14+
waitForSpecToFinish(expectedResults?: {
15+
passCount?: number
16+
failCount?: number
17+
pendingCount?: number
18+
}): void
1319
}
1420
}
1521
}
1622

1723
// Here we export the function with no intention to import it
1824
// This only tells the typescript type checker that this definitely is a module
1925
// This way, we are allowed to use the global namespace declaration
20-
export const waitForSpecToFinish = () => {
26+
export const waitForSpecToFinish = (expectedResults) => {
2127
// First ensure the test is loaded
2228
cy.get('.passed > .num').should('contain', '--')
2329
cy.get('.failed > .num').should('contain', '--')
@@ -27,6 +33,10 @@ export const waitForSpecToFinish = () => {
2733

2834
// Then ensure the tests have finished
2935
cy.get('[aria-label="Rerun all tests"]', { timeout: 30000 })
36+
37+
if (expectedResults) {
38+
shouldHaveTestResults(expectedResults)
39+
}
3040
}
3141

3242
Cypress.Commands.add('waitForSpecToFinish', waitForSpecToFinish)

‎packages/app/src/pages/Specs/Runner.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ const configChangeHandler: SubscriptionHandlerArg<any, any> = (
104104
window.__CYPRESS_CONFIG__ = next.configChange.serveConfig
105105
106106
const eventManager = useEventManager()
107+
const isRerun = true
107108
108-
eventManager.runSpec()
109+
eventManager.runSpec(isRerun)
109110
} catch (e) {
110111
// eventManager may not be defined, for example if the spec
111112
// is still loading.

‎packages/app/src/runner/SnapshotControls.cy.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ describe('SnapshotControls', { viewportHeight: 200, viewportWidth: 500 }, () =>
8686
// simulate it by registering the same unpin:snapshot event it does.
8787
eventManager.on('unpin:snapshot', () => snapshotStore.$reset())
8888

89-
// debugger
90-
// console.log('snapshotWithSnapshots', snapshotWithSnapshots)
9189
snapshotStore.pinSnapshot({ ...snapshotWithSnapshots, $el: document.body })
9290

9391
mountSnapshotControls(eventManager, autIframe)

‎packages/app/src/runner/event-manager.ts

+16-19
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class EventManager {
9090
return
9191
}
9292

93-
return this.runSpec(state)
93+
return this.rerunSpec()
9494
}
9595

9696
const connectionInfo: AddGlobalListenerOptions = {
@@ -719,15 +719,19 @@ export class EventManager {
719719
this.ws.off()
720720
}
721721

722-
async teardown (state: MobxRunnerStore) {
722+
async teardown (state: MobxRunnerStore, isRerun = false) {
723723
if (!Cypress) {
724724
return
725725
}
726726

727727
state.setIsLoading(true)
728728

729-
// when we are re-running we first
730-
// need to stop cypress always
729+
if (!isRerun) {
730+
// only clear session state when a new spec is selected
731+
Cypress.backend('reset:session:state')
732+
}
733+
734+
// when we are re-running we first need to stop cypress always
731735
Cypress.stop()
732736
// Clean up the primary communicator to prevent possible memory leaks / dangling references before the Cypress instance is destroyed.
733737
Cypress.primaryOriginCommunicator.removeAllListeners()
@@ -744,28 +748,21 @@ export class EventManager {
744748
})
745749
}
746750

747-
async _rerun () {
751+
async rerunSpec () {
752+
if (!this || !Cypress) {
753+
// if the tests have been reloaded then there is nothing to rerun
754+
return
755+
}
756+
748757
await this.resetReporter()
749758

750-
// this probably isn't 100% necessary
751-
// since Cypress will fall out of scope
752-
// but we want to be aggressive here
753-
// and force GC early and often
759+
// this probably isn't 100% necessary since Cypress will fall out of scope
760+
// but we want to be aggressive here and force GC early and often
754761
Cypress.removeAllListeners()
755762

756763
this.localBus.emit('restart')
757764
}
758765

759-
async runSpec (state: MobxRunnerStore) {
760-
if (!Cypress) {
761-
return
762-
}
763-
764-
await this.teardown(state)
765-
766-
return this._rerun()
767-
}
768-
769766
_interceptStudio (displayProps) {
770767
if (this.studioRecorder.isActive) {
771768
displayProps.hookId = this.studioRecorder.hookId

‎packages/app/src/runner/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ function getSpecUrl (namespace: string, specSrc: string) {
165165
* This should be called before you execute a spec,
166166
* or re-running the current spec.
167167
*/
168-
function teardownSpec () {
168+
function teardownSpec (isRerun: boolean = false) {
169169
useSnapshotStore().$reset()
170170

171-
return getEventManager().teardown(getMobxRunnerStore())
171+
return getEventManager().teardown(getMobxRunnerStore(), isRerun)
172172
}
173173

174174
let isTorndown = false
@@ -399,8 +399,8 @@ async function initialize () {
399399
* 5. Setup the spec. This involves a few things, see the `runSpecCT` function's
400400
* description for more information.
401401
*/
402-
async function executeSpec (spec: SpecFile) {
403-
await teardownSpec()
402+
async function executeSpec (spec: SpecFile, isRerun: boolean = false) {
403+
await teardownSpec(isRerun)
404404

405405
const mobxRunnerStore = getMobxRunnerStore()
406406

‎packages/app/src/runner/useEventManager.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,23 @@ export function useEventManager () {
99
const autStore = useAutStore()
1010
const specStore = useSpecStore()
1111

12-
function runSpec () {
12+
function runSpec (isRerun: boolean = false) {
1313
if (!specStore.activeSpec) {
1414
throw Error(`Cannot run spec when specStore.active spec is null or undefined!`)
1515
}
1616

1717
autStore.setScriptError(null)
18-
UnifiedRunnerAPI.executeSpec(specStore.activeSpec)
18+
UnifiedRunnerAPI.executeSpec(specStore.activeSpec, isRerun)
1919
}
2020

2121
function initializeRunnerLifecycleEvents () {
2222
// these events do not use GraphQL
2323
eventManager.on('restart', () => {
2424
// If we get the event to restart but have already navigated away from the runner, don't execute the spec
2525
if (specStore.activeSpec) {
26-
runSpec()
26+
const isRerun = true
27+
28+
runSpec(isRerun)
2729
}
2830
})
2931

‎packages/driver/src/cypress.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,12 @@ class $Cypress {
575575
return this.emit('after:all:screenshots', ...args)
576576

577577
case 'command:log:added':
578-
this.runner.addLog(args[0], this.config('isInteractive'))
578+
this.runner?.addLog(args[0], this.config('isInteractive'))
579579

580580
return this.emit('log:added', ...args)
581581

582582
case 'command:log:changed':
583-
this.runner.addLog(args[0], this.config('isInteractive'))
583+
this.runner?.addLog(args[0], this.config('isInteractive'))
584584

585585
return this.emit('log:changed', ...args)
586586

‎packages/driver/src/dom/selection.ts

-6
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,6 @@ const _moveCursorToLineStartOrEnd = function (toStart: boolean, el: HTMLElement)
420420

421421
return
422422
}
423-
// const doc = $document.getDocumentFromElement(el)
424-
// console.log(doc.activeElement)
425-
// $elements.callNativeMethod(doc, 'execCommand', 'selectall', false)
426-
// $elements.callNativeMethod(el, 'select')
427-
// _getSelectionByEl(el).ca
428-
// toStart ? _getSelectionByEl(el).collapseToStart : _getSelectionByEl(el).collapseToEnd()
429423

430424
if (isTextarea) {
431425
const bounds = _getSelectionBoundsFromTextarea(el)

‎packages/server/lib/socket-base.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,7 @@ export class SocketBase {
466466
case 'get:rendered:html:origins':
467467
return options.getRenderedHTMLOrigins()
468468
case 'reset:rendered:html:origins': {
469-
resetRenderedHTMLOrigins()
470-
471-
return
469+
return resetRenderedHTMLOrigins()
472470
}
473471
case 'cross:origin:bridge:ready':
474472
return this.localBus.emit('cross:origin:bridge:ready', args[0])
@@ -479,9 +477,7 @@ export class SocketBase {
479477
case 'cross:origin:automation:cookies:received':
480478
return this.localBus.emit('cross:origin:automation:cookies:received')
481479
default:
482-
throw new Error(
483-
`You requested a backend event we cannot handle: ${eventName}`,
484-
)
480+
throw new Error(`You requested a backend event we cannot handle: ${eventName}`)
485481
}
486482
}
487483

‎scripts/gulp/utils/childProcessUtils.ts

-9
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ export async function spawned (
9595

9696
const [executable, ...rest] = command.split(' ')
9797

98-
// console.log(useExecutable, rest, spawnOpts)
99-
10098
const cp = universalSpawn(executable, rest, {
10199
stdio: 'pipe',
102100
...spawnOpts,
@@ -133,13 +131,6 @@ export async function forked (
133131
) {
134132
const { waitForExit, waitForData, tapErr, tapOut, ...spawnOpts } = opts
135133

136-
// console.log(args)
137-
138-
// let useExecutable = executable
139-
// if (process.platform === 'win32' && !useExecutable.endsWith('.cmd')) {
140-
// useExecutable = `${executable}.cmd`
141-
// }
142-
143134
const cp = fork(modulePath, args, {
144135
stdio: 'pipe',
145136
...spawnOpts,

‎system-tests/projects/hooks-after-rerun/cypress/e2e/runnable-run-count.cy.js

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const urls = [
55
]
66

77
function incrState (key) {
8-
// console.log(key)
98
cy.log(key)
109
cy.task('incrState', key)
1110
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
it('t1', () => {
22
const setupFn = cy.stub().as('runSetup')
33

4-
cy.session('blank_session', setupFn)
4+
cy.session('user1', setupFn)
55
cy.log('after')
66
})

0 commit comments

Comments
 (0)