From 8c3a68d80a17fd33698343ebc8b6ac8d0afc4ad2 Mon Sep 17 00:00:00 2001 From: Emily Rohrbough Date: Tue, 20 Sep 2022 08:08:44 -0500 Subject: [PATCH] fix tests --- cli/types/cypress.d.ts | 18 ++++++++++++-- packages/app/src/runner/index.ts | 24 +++++++++++-------- .../e2e/commands/sessions/manager.cy.ts | 8 +++++++ .../driver/src/cy/commands/sessions/index.ts | 5 ++-- system-tests/test/session_spec.ts | 1 - 5 files changed, 41 insertions(+), 15 deletions(-) diff --git a/cli/types/cypress.d.ts b/cli/types/cypress.d.ts index 905d9383fa..c3f65c7f58 100644 --- a/cli/types/cypress.d.ts +++ b/cli/types/cypress.d.ts @@ -655,8 +655,22 @@ declare namespace Cypress { } interface SessionOptions { + /** + * Whether or not to persist the session across all specs in the run. + * @default {false} + */ cacheAcrossSpecs?: boolean, - validate?: () => false | void + /** + * Function to run immediately after the session is created and `setup` function runs or + * after a session is restored and the page is cleared. If this returns `false`, throws an + * exception, returns a Promise which resolves to `false` or rejects or contains any failing + * Cypress command, the session is considered invalid. + * + * If validation fails immediately after `setup`, the test will fail. + * If validation fails after restoring a session, `setup` will re-run. + * @default {false} + */ + validate?: () => Promise | false | void } type CanReturnChainable = void | Chainable | Promise @@ -1075,7 +1089,7 @@ declare namespace Cypress { * * @see https://on.cypress.io/session */ - session(id: string | object, setup?: SessionOptions['validate'], options?: SessionOptions): Chainable + session(id: string | object, setup?: () => void, options?: SessionOptions): Chainable /** * Get the window.document of the page that is currently active. diff --git a/packages/app/src/runner/index.ts b/packages/app/src/runner/index.ts index 80d45c26ba..a39ccb20d7 100644 --- a/packages/app/src/runner/index.ts +++ b/packages/app/src/runner/index.ts @@ -176,17 +176,11 @@ function getSpecUrl (namespace: string, specSrc: string) { * or re-running the current spec. */ function teardownSpec (isRerun: boolean = false) { - useAutStore().$reset() - - useStudioStore().cancel() - useSnapshotStore().$reset() _eventManager?.stop() - getEventManager().teardown(getMobxRunnerStore(), isRerun) - - return + return getEventManager().teardown(getMobxRunnerStore(), isRerun) } let isTorndown = false @@ -197,8 +191,9 @@ let isTorndown = false * any associated events. */ export async function teardown () { - teardownSpec(false) UnifiedReporterAPI.setInitializedReporter(false) + _eventManager?.stop() + _eventManager?.teardown(getMobxRunnerStore()) await _eventManager?.resetReporter() _eventManager = undefined isTorndown = true @@ -355,8 +350,15 @@ async function initialize () { return } + // Reset stores const autStore = useAutStore() + autStore.$reset() + + const studioStore = useStudioStore() + + studioStore.cancel() + // TODO(lachlan): UNIFY-1318 - use GraphQL to get the viewport dimensions // once it is more practical to do so // find out if we need to continue managing viewportWidth/viewportHeight in MobX at all. @@ -395,9 +397,11 @@ async function initialize () { * description for more information. */ async function executeSpec (spec: SpecFile, isRerun: boolean = false) { - await Promise.all([teardownSpec(isRerun), UnifiedReporterAPI.resetReporter()]) + await teardownSpec(isRerun) - getMobxRunnerStore().setSpec(spec) + const mobxRunnerStore = getMobxRunnerStore() + + mobxRunnerStore.setSpec(spec) UnifiedReporterAPI.setupReporter() diff --git a/packages/driver/cypress/e2e/commands/sessions/manager.cy.ts b/packages/driver/cypress/e2e/commands/sessions/manager.cy.ts index a23fa5dcba..71f1c5513b 100644 --- a/packages/driver/cypress/e2e/commands/sessions/manager.cy.ts +++ b/packages/driver/cypress/e2e/commands/sessions/manager.cy.ts @@ -28,6 +28,7 @@ describe('src/cy/commands/sessions/manager.ts', () => { 'session_1': { id: 'session_1', setup: () => {}, + cacheAcrossSpecs: false, hydrated: true, }, } @@ -47,11 +48,13 @@ describe('src/cy/commands/sessions/manager.ts', () => { 'session_1': { id: 'session_1', setup: () => {}, + cacheAcrossSpecs: false, hydrated: false, }, 'session_2': { id: 'session_2', setup: () => {}, + cacheAcrossSpecs: false, hydrated: true, }, } @@ -62,6 +65,7 @@ describe('src/cy/commands/sessions/manager.ts', () => { 'session_3': { id: 'session_3', setup: () => {}, + cacheAcrossSpecs: false, hydrated: true, }, } @@ -96,11 +100,13 @@ describe('src/cy/commands/sessions/manager.ts', () => { 'session_1': { id: 'session_1', setup: () => {}, + cacheAcrossSpecs: false, hydrated: false, }, 'session_2': { id: 'session_2', setup: () => {}, + cacheAcrossSpecs: false, hydrated: true, }, } @@ -135,11 +141,13 @@ describe('src/cy/commands/sessions/manager.ts', () => { 'session_1': { id: 'session_1', setup: () => {}, + cacheAcrossSpecs: false, hydrated: false, }, 'session_2': { id: 'session_2', setup: () => {}, + cacheAcrossSpecs: false, hydrated: true, }, } diff --git a/packages/driver/src/cy/commands/sessions/index.ts b/packages/driver/src/cy/commands/sessions/index.ts index 5a709882ef..ce5ffedf85 100644 --- a/packages/driver/src/cy/commands/sessions/index.ts +++ b/packages/driver/src/cy/commands/sessions/index.ts @@ -8,6 +8,7 @@ import { getConsoleProps, navigateAboutBlank, } from './utils' +import type { ServerSessionData } from '@packages/types' type SessionData = Cypress.Commands.Session.SessionData @@ -21,7 +22,7 @@ type SessionData = Cypress.Commands.Session.SessionData */ export default function (Commands, Cypress, cy) { - Object.values(Cypress.state('activeSessions') || {}).forEach((sessionData) => { + Object.values(Cypress.state('activeSessions') || {}).forEach((sessionData: ServerSessionData) => { if (sessionData.cacheAcrossSpecs) { sessionsManager.registeredSessions.set(sessionData.id, true) } @@ -61,7 +62,7 @@ export default function (Commands, Cypress, cy) { }) Commands.addAll({ - session (id, setup?: Function, options: Cypress.SessionOptions = { cacheAcrossSpecs: false }) { + session (id: string | object, setup?: () => void, options: Cypress.SessionOptions = { cacheAcrossSpecs: false }) { throwIfNoSessionSupport() if (!id || !_.isString(id) && !_.isObject(id)) { diff --git a/system-tests/test/session_spec.ts b/system-tests/test/session_spec.ts index 28683432cc..360d37be11 100644 --- a/system-tests/test/session_spec.ts +++ b/system-tests/test/session_spec.ts @@ -145,7 +145,6 @@ describe('e2e sessions', () => { project: 'session-and-origin-e2e-specs', spec: 'session_persist_1.cy.js,session_persist_2.cy.js', browser: '!webkit', // TODO(webkit): fix+unskip (needs multidomain support) - spec: 'session_persist_spec_1.cy.js,session_persist_spec_2.cy.js', snapshot: true, config: { experimentalSessionAndOrigin: true,