cypress-io/cypress · error · Error

cy.session from a component spec is not allowed

Error message

cy.session from a component spec is not allowed

What it means

Thrown by @cypress/mount-utils setupHooks, which overwrites the `session` command during component testing. cy.session caches and restores cross-spec session state (cookies, localStorage) tied to a visited origin; in CT there is no visited page to scope a session to, and restoring arbitrary state would corrupt the freshly mounted component. The overwrite throws on any invocation.

Source

Thrown at npm/mount-utils/src/index.ts:40

export function setupHooks (optionalCallback?: Function) {
  // We don't want CT side effects to run when e2e
  // testing so we early return.
  // System test to verify CT side effects do not pollute e2e: system-tests/test/e2e_with_mount_import_spec.ts
  if (Cypress.testingType !== 'component') {
    return
  }

  // When running component specs, we cannot allow "cy.visit"
  // because it will wipe out our preparation work, and does not make much sense
  // thus we overwrite "cy.visit" to throw an error
  Cypress.Commands.overwrite('visit', () => {
    throw new Error(
      'cy.visit from a component spec is not allowed',
    )
  })

  Cypress.Commands.overwrite('session', () => {
    throw new Error(
      'cy.session from a component spec is not allowed',
    )
  })

  Cypress.Commands.overwrite('origin', () => {
    throw new Error(
      'cy.origin from a component spec is not allowed',
    )
  })

  // @ts-ignore
  Cypress.on('test:before:after:run:async', () => {
    optionalCallback?.()
  })
}

View on GitHub (pinned to 0d85fdc912)

Solutions

  1. Do not use cy.session in component specs; set the state the component needs directly via props or cy.window() mutations.
  2. Stub auth/session dependencies at the component level (e.g. mock a useAuth hook) rather than via cy.session.
  3. Relocate any cy.session logic to e2e specs where a real origin is visited.
  4. Gate shared session helpers behind a testingType check.

Example fix

// before (component spec)
beforeEach(() => {
  cy.session('user', () => { cy.window().then(w => w.localStorage.setItem('token', 'abc')) })
})
// after
beforeEach(() => {
  cy.window().then(w => w.localStorage.setItem('token', 'abc'))
})
Defensive patterns

Strategy: type-guard

Validate before calling

if (Cypress.testingType === 'e2e') {
  cy.session(id, setup)
}

Type guard

const isE2E = (): boolean => Cypress.testingType === 'e2e'

Prevention

When it happens

Trigger: Calling `cy.session(id, setup)` inside a component spec. Fires whenever setupHooks has registered the CT overwrites (always, for testingType='component').

Common situations: Porting an e2e auth pattern (cy.session to persist login) into CT; a shared commands file that wraps session for reuse across both spec types; using a third-party plugin that internally calls cy.session.

Related errors


AI-assisted analysis of cypress-io/cypress@0d85fdc912 (2026-08-12). Data as JSON: /api/errors/d40665ceaf90a223. Report an issue: GitHub.