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 all 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).

**Bugfixes:**

- Fixed an issue where the configuration setting `trashAssetsBeforeRuns=false` was ignored for assets in the `videosFolder`. These assets were incorrectly deleted before running tests with `cypress run`. Addresses [#8280](https://github.com/cypress-io/cypress/issues/8280).
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)
})
})
})
1 change: 0 additions & 1 deletion packages/app/src/runner/events/capture-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export const addCaptureProtocolListeners = (Cypress: Cypress.Cypress) => {
}

Cypress.on('viewport:changed', viewportChangedHandler)
// @ts-expect-error
Cypress.primaryOriginCommunicator.on('viewport:changed', viewportChangedHandler)

Cypress.on('test:before:run:async', async (attributes) => {
Expand Down
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 type EventEmitter from 'events'

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

type GetCypressFunction = () => Cypress.Cypress

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()
}
7 changes: 7 additions & 0 deletions packages/driver/types/internal-types-lite.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ declare namespace Cypress {
interface Cypress {
runner: any
state: State
emit: import('eventemitter2').EventEmitter2['emit']
removeListener: import('eventemitter2').EventEmitter2['removeListener']
primaryOriginCommunicator: import('eventemitter2').EventEmitter2 & {
toSpecBridge: (origin: string, event: string, data?: any, responseEvent?: string) => void
userInvocationStack?: string
}
}

interface Actions {
Expand All @@ -23,6 +29,7 @@ declare namespace Cypress {
(action: 'test:after:run:async', fn: (attributes: ObjectLike, test: Mocha.Test) => void)
(action: 'cy:protocol-snapshot', fn: () => void)
(action: 'test:before:after:run:async', fn: (attributes: ObjectLike, test: Mocha.Test, options: ObjectLike) => void | Promise<any>): Cypress
(action: 'paused', fn: (nextCommandName: string) => void)
}

interface Backend {
Expand Down
5 changes: 1 addition & 4 deletions packages/driver/types/internal-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ declare namespace Cypress {
}
sinon: sinon.SinonApi
utils: CypressUtils
state: State
events: Events
emit: (event: string, payload?: any) => void
primaryOriginCommunicator: import('../src/cross-origin/communicator').PrimaryOriginCommunicator
specBridgeCommunicator: import('../src/cross-origin/communicator').SpecBridgeCommunicator
mocha: $Mocha
configure: (config: Cypress.ObjectLike) => void
Expand Down Expand Up @@ -96,7 +93,7 @@ declare namespace Cypress {
(action: 'before:stability:release', fn: () => void)
(action: '_log:added', fn: (attributes: ObjectLike, log: Cypress.Log) => void): Cypress
(action: '_log:changed', fn: (attributes: ObjectLike, log: Cypress.Log) => void): Cypress
(action: 'paused', fn: (nextCommandName: string) => void)
(action: 'resume:all', fn: () => void)
}

interface Backend {
Expand Down
Loading