Skip to content

Commit

Permalink
fix: clear session state when changing specs in open mode (#23146)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyrohrbough committed Aug 12, 2022
1 parent 0729a68 commit f1122fc
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 62 deletions.
1 change: 0 additions & 1 deletion npm/eslint-plugin-dev/test/no-return-before.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ describe(ruleName, () => {
})

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

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

Expand Down
2 changes: 0 additions & 2 deletions npm/eslint-plugin-dev/test/skip-comment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ describe('skip-comment', () => {
})

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

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

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

expect(result.messages[0].message).toContain('it')
expect(result.messages[0].message).toContain('FOOBAR:')
Expand Down
1 change: 0 additions & 1 deletion npm/webpack-preprocessor/test/e2e/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ exports.runTest = async (options = {}) => {
})
}

// console.log(stdout)
console.log(`${chalk.bold('run matched these results:')} ${JSON.stringify(opts.expectedResults, null, 2)}`)
})
}
Expand Down
64 changes: 62 additions & 2 deletions packages/app/cypress/e2e/runner/sessions.ui.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ describe('runner/cypress sessions.ui.spec', {
passCount: 1,
})

validateSessionsInstrumentPanel(['blank_session'])
validateSessionsInstrumentPanel(['user1'])

cy.get('.command-name-session')
.within(() => {
cy.get('.command-expander').first().click()
cy.contains('blank_session')
cy.contains('user1')
cy.contains('created')

validateSetupSessionGroup()
Expand Down Expand Up @@ -309,3 +309,63 @@ describe('runner/cypress sessions.ui.spec', {
cy.percySnapshot()
})
})

describe('runner/cypress sessions.open_mode.spec', () => {
beforeEach(() => {
cy.scaffoldProject('session-and-origin-e2e-specs')
cy.openProject('session-and-origin-e2e-specs')
cy.startAppServer('e2e')
cy.visitApp()

cy.get('[data-cy-row="multiple_sessions.cy.js"]').click()
cy.waitForSpecToFinish({
passCount: 1,
})

cy.get('.command-name-session').should('contain', 'user1')
.find('.reporter-tag').should('contain', 'created')

cy.get('.command-name-session').should('contain', 'user2')
.find('.reporter-tag').should('contain', 'created')
})

it('persists spec sessions when clicking "rerun all tests" button', () => {
cy.get('.restart').click()

cy.waitForSpecToFinish({
passCount: 1,
})

cy.get('.command-name-session').should('contain', 'user1')
.find('.reporter-tag').should('contain', 'restored')

cy.get('.command-name-session').should('contain', 'user2')
.find('.reporter-tag').should('contain', 'restored')
})

it('persists spec sessions on refresh', () => {
cy.get('body').type('r')

cy.waitForSpecToFinish({
passCount: 1,
})

cy.get('.command-name-session').should('contain', 'user1')
.find('.reporter-tag').should('contain', 'restored')

cy.get('.command-name-session').should('contain', 'user2')
.find('.reporter-tag').should('contain', 'restored')
})

it('does not persists spec sessions when selecting a different spec', () => {
cy.get('body').type('f')
cy.get('div[title="new_session.cy.js"]').click()

cy.waitForSpecToFinish({
passCount: 1,
})

cy.get('.command-name-session').should('contain', 'user1')
.find('.reporter-tag').should('contain', 'created')
})
})
14 changes: 12 additions & 2 deletions packages/app/cypress/e2e/support/execute-spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { shouldHaveTestResults } from '../runner/support/spec-loader'

declare global {
namespace Cypress {
interface Chainable {
Expand All @@ -9,15 +11,19 @@ declare global {
* 3. Waits (with a timeout of 30s) for the Rerun all tests button to be present. This ensures all tests have completed
*
*/
waitForSpecToFinish(): void
waitForSpecToFinish(expectedResults?: {
passCount?: number
failCount?: number
pendingCount?: number
}): void
}
}
}

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

// Then ensure the tests have finished
cy.get('[aria-label="Rerun all tests"]', { timeout: 30000 })

if (expectedResults) {
shouldHaveTestResults(expectedResults)
}
}

Cypress.Commands.add('waitForSpecToFinish', waitForSpecToFinish)
3 changes: 2 additions & 1 deletion packages/app/src/pages/Specs/Runner.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ const configChangeHandler: SubscriptionHandlerArg<any, any> = (
window.__CYPRESS_CONFIG__ = next.configChange.serveConfig
const eventManager = useEventManager()
const isRerun = true
eventManager.runSpec()
eventManager.runSpec(isRerun)
} catch (e) {
// eventManager may not be defined, for example if the spec
// is still loading.
Expand Down
2 changes: 0 additions & 2 deletions packages/app/src/runner/SnapshotControls.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ describe('SnapshotControls', { viewportHeight: 200, viewportWidth: 500 }, () =>
// simulate it by registering the same unpin:snapshot event it does.
eventManager.on('unpin:snapshot', () => snapshotStore.$reset())

// debugger
// console.log('snapshotWithSnapshots', snapshotWithSnapshots)
snapshotStore.pinSnapshot({ ...snapshotWithSnapshots, $el: document.body })

mountSnapshotControls(eventManager, autIframe)
Expand Down
35 changes: 16 additions & 19 deletions packages/app/src/runner/event-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class EventManager {
return
}

return this.runSpec(state)
return this.rerunSpec()
}

const connectionInfo: AddGlobalListenerOptions = {
Expand Down Expand Up @@ -719,15 +719,19 @@ export class EventManager {
this.ws.off()
}

async teardown (state: MobxRunnerStore) {
async teardown (state: MobxRunnerStore, isRerun = false) {
if (!Cypress) {
return
}

state.setIsLoading(true)

// when we are re-running we first
// need to stop cypress always
if (!isRerun) {
// only clear session state when a new spec is selected
Cypress.backend('reset:session:state')
}

// when we are re-running we first need to stop cypress always
Cypress.stop()
// Clean up the primary communicator to prevent possible memory leaks / dangling references before the Cypress instance is destroyed.
Cypress.primaryOriginCommunicator.removeAllListeners()
Expand All @@ -744,28 +748,21 @@ export class EventManager {
})
}

async _rerun () {
async rerunSpec () {
if (!this || !Cypress) {
// if the tests have been reloaded then there is nothing to rerun
return
}

await this.resetReporter()

// this probably isn't 100% necessary
// since Cypress will fall out of scope
// but we want to be aggressive here
// and force GC early and often
// this probably isn't 100% necessary since Cypress will fall out of scope
// but we want to be aggressive here and force GC early and often
Cypress.removeAllListeners()

this.localBus.emit('restart')
}

async runSpec (state: MobxRunnerStore) {
if (!Cypress) {
return
}

await this.teardown(state)

return this._rerun()
}

_interceptStudio (displayProps) {
if (this.studioRecorder.isActive) {
displayProps.hookId = this.studioRecorder.hookId
Expand Down
8 changes: 4 additions & 4 deletions packages/app/src/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ function getSpecUrl (namespace: string, specSrc: string) {
* This should be called before you execute a spec,
* or re-running the current spec.
*/
function teardownSpec () {
function teardownSpec (isRerun: boolean = false) {
useSnapshotStore().$reset()

return getEventManager().teardown(getMobxRunnerStore())
return getEventManager().teardown(getMobxRunnerStore(), isRerun)
}

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

const mobxRunnerStore = getMobxRunnerStore()

Expand Down
8 changes: 5 additions & 3 deletions packages/app/src/runner/useEventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ export function useEventManager () {
const autStore = useAutStore()
const specStore = useSpecStore()

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

autStore.setScriptError(null)
UnifiedRunnerAPI.executeSpec(specStore.activeSpec)
UnifiedRunnerAPI.executeSpec(specStore.activeSpec, isRerun)
}

function initializeRunnerLifecycleEvents () {
// these events do not use GraphQL
eventManager.on('restart', () => {
// If we get the event to restart but have already navigated away from the runner, don't execute the spec
if (specStore.activeSpec) {
runSpec()
const isRerun = true

runSpec(isRerun)
}
})

Expand Down
4 changes: 2 additions & 2 deletions packages/driver/src/cypress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,12 @@ class $Cypress {
return this.emit('after:all:screenshots', ...args)

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

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

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

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

Expand Down
6 changes: 0 additions & 6 deletions packages/driver/src/dom/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,6 @@ const _moveCursorToLineStartOrEnd = function (toStart: boolean, el: HTMLElement)

return
}
// const doc = $document.getDocumentFromElement(el)
// console.log(doc.activeElement)
// $elements.callNativeMethod(doc, 'execCommand', 'selectall', false)
// $elements.callNativeMethod(el, 'select')
// _getSelectionByEl(el).ca
// toStart ? _getSelectionByEl(el).collapseToStart : _getSelectionByEl(el).collapseToEnd()

if (isTextarea) {
const bounds = _getSelectionBoundsFromTextarea(el)
Expand Down
8 changes: 2 additions & 6 deletions packages/server/lib/socket-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,7 @@ export class SocketBase {
case 'get:rendered:html:origins':
return options.getRenderedHTMLOrigins()
case 'reset:rendered:html:origins': {
resetRenderedHTMLOrigins()

return
return resetRenderedHTMLOrigins()
}
case 'cross:origin:bridge:ready':
return this.localBus.emit('cross:origin:bridge:ready', args[0])
Expand All @@ -479,9 +477,7 @@ export class SocketBase {
case 'cross:origin:automation:cookies:received':
return this.localBus.emit('cross:origin:automation:cookies:received')
default:
throw new Error(
`You requested a backend event we cannot handle: ${eventName}`,
)
throw new Error(`You requested a backend event we cannot handle: ${eventName}`)
}
}

Expand Down
9 changes: 0 additions & 9 deletions scripts/gulp/utils/childProcessUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ export async function spawned (

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

// console.log(useExecutable, rest, spawnOpts)

const cp = universalSpawn(executable, rest, {
stdio: 'pipe',
...spawnOpts,
Expand Down Expand Up @@ -133,13 +131,6 @@ export async function forked (
) {
const { waitForExit, waitForData, tapErr, tapOut, ...spawnOpts } = opts

// console.log(args)

// let useExecutable = executable
// if (process.platform === 'win32' && !useExecutable.endsWith('.cmd')) {
// useExecutable = `${executable}.cmd`
// }

const cp = fork(modulePath, args, {
stdio: 'pipe',
...spawnOpts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const urls = [
]

function incrState (key) {
// console.log(key)
cy.log(key)
cy.task('incrState', key)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
it('t1', () => {
const setupFn = cy.stub().as('runSetup')

cy.session('blank_session', setupFn)
cy.session('user1', setupFn)
cy.log('after')
})

5 comments on commit f1122fc

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f1122fc Aug 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.5.0/linux-x64/develop-f1122fcf62c14abddc0ae96116572402c1be2372/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f1122fc Aug 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.5.0/linux-arm64/develop-f1122fcf62c14abddc0ae96116572402c1be2372/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f1122fc Aug 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.5.0/darwin-arm64/develop-f1122fcf62c14abddc0ae96116572402c1be2372/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f1122fc Aug 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.5.0/darwin-x64/develop-f1122fcf62c14abddc0ae96116572402c1be2372/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f1122fc Aug 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.5.0/win32-x64/develop-f1122fcf62c14abddc0ae96116572402c1be2372/cypress.tgz

Please sign in to comment.