Skip to content

perf: ensure the pausing event listeners are removed #31596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

_Released 5/6/2025 (PENDING)_

**Performance:**

- Ensure the previous pausing event handlers are removed before new ones are added. Addressed in [#31596](https://github.com/cypress-io/cypress/pull/31596).

**Misc:**

- The URL in the Cypress App no longer displays a white background when the URL is loading. Fixes [#31556](https://github.com/cypress-io/cypress/issues/31556).
Expand Down
28 changes: 26 additions & 2 deletions packages/app/cypress/e2e/runner/event-manager.cy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { loadSpec } from './support/spec-loader'
import { loadSpec, shouldHaveTestResults } from './support/spec-loader'

describe('event-manager', () => {
it('emits the cypress:created event when spec is rerun', () => {
// load the spec initially
loadSpec({
filePath: 'hooks/basic.cy.js',
passCount: 1,
passCount: 2,
})

cy.window().then((win) => {
Expand All @@ -26,4 +26,28 @@ describe('event-manager', () => {
cy.wrap(() => eventReceived).invoke('call').should('be.true')
})
})

it('clears the pause listeners when the spec is rerun', () => {
loadSpec({
filePath: 'hooks/basic.cy.js',
passCount: 2,
})

cy.window().then((win) => {
const eventManager = win.getEventManager()

cy.wrap(() => eventManager.reporterBus.listeners('runner:next').length).invoke('call').should('equal', 1)

// trigger a rerun
cy.get('.restart').click()

shouldHaveTestResults({
passCount: 2,
failCount: 0,
pendingCount: 0,
})

cy.wrap(() => eventManager.reporterBus.listeners('runner:next').length).invoke('call').should('equal', 1)
})
})
})
74 changes: 54 additions & 20 deletions packages/app/src/runner/events/pausing.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
export const handlePausing = (getCypress, reporterBus) => {
const Cypress = getCypress()
// tracks whether the cy.pause() was called from the primary driver
// (value === null) or from a cross-origin spec bridge (value is the origin
// matching that spec bridge)
let sendEventsToOrigin = null
// tracks whether the cy.pause() was called from the primary driver
// (value === null) or from a cross-origin spec bridge (value is the origin

reporterBus.on('runner:next', () => {
const Cypress = getCypress()
import EventEmitter from 'events'

// matching that spec bridge)
let sendEventsToOrigin: string | null = null

type GetCypressFunction = () => any

class PauseHandlers {
constructor (private getCypress: GetCypressFunction, private reporterBus: EventEmitter) {}

nextHandler = () => {
const Cypress = this.getCypress()

if (!Cypress) return

Expand All @@ -17,10 +23,10 @@ export const handlePausing = (getCypress, reporterBus) => {
} else {
Cypress.emit('resume:next')
}
})
}

reporterBus.on('runner:resume', () => {
const Cypress = getCypress()
resumeHandler = () => {
const Cypress = this.getCypress()

if (!Cypress) return

Expand All @@ -34,17 +40,45 @@ export const handlePausing = (getCypress, reporterBus) => {

// pause sequence is over - reset this for subsequent pauses
sendEventsToOrigin = null
})
}

// from the primary driver
Cypress.on('paused', (nextCommandName) => {
reporterBus.emit('paused', nextCommandName)
})
pausedHandler = (nextCommandName: string) => {
this.reporterBus.emit('paused', nextCommandName)
}

// from a cross-origin spec bridge
Cypress.primaryOriginCommunicator.on('paused', ({ nextCommandName, origin }) => {
crossOriginPausedHandler = ({ nextCommandName, origin }: { nextCommandName: string, origin: string }) => {
sendEventsToOrigin = origin
this.reporterBus.emit('paused', nextCommandName)
}

removeListeners = () => {
const Cypress = this.getCypress()

this.reporterBus.removeListener('runner:next', this.nextHandler)
this.reporterBus.removeListener('runner:resume', this.resumeHandler)
Cypress.removeListener('paused', this.pausedHandler)
Cypress.primaryOriginCommunicator.removeListener('paused', this.crossOriginPausedHandler)
}

addListeners = () => {
const Cypress = this.getCypress()

this.reporterBus.on('runner:next', this.nextHandler)
this.reporterBus.on('runner:resume', this.resumeHandler)
Cypress.on('paused', this.pausedHandler)
Cypress.primaryOriginCommunicator.on('paused', this.crossOriginPausedHandler)
}
}

let currentHandlers: PauseHandlers | null = null

export const handlePausing = (getCypress: GetCypressFunction, reporterBus: EventEmitter) => {
// Remove existing handlers if they exist
if (currentHandlers) {
currentHandlers.removeListeners()
}

reporterBus.emit('paused', nextCommandName)
})
// Create new handlers
currentHandlers = new PauseHandlers(getCypress, reporterBus)
currentHandlers.addListeners()
}
Loading