{"record":{"id":"d40665ceaf90a223","repo":"cypress-io/cypress","slug":"cy-session-from-a-component-spec-is-not-allowed","errorCode":null,"errorMessage":"cy.session from a component spec is not allowed","messagePattern":"cy\\.session from a component spec is not allowed","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"npm/mount-utils/src/index.ts","lineNumber":40,"sourceCode":"export function setupHooks (optionalCallback?: Function) {\n  // We don't want CT side effects to run when e2e\n  // testing so we early return.\n  // System test to verify CT side effects do not pollute e2e: system-tests/test/e2e_with_mount_import_spec.ts\n  if (Cypress.testingType !== 'component') {\n    return\n  }\n\n  // When running component specs, we cannot allow \"cy.visit\"\n  // because it will wipe out our preparation work, and does not make much sense\n  // thus we overwrite \"cy.visit\" to throw an error\n  Cypress.Commands.overwrite('visit', () => {\n    throw new Error(\n      'cy.visit from a component spec is not allowed',\n    )\n  })\n\n  Cypress.Commands.overwrite('session', () => {\n    throw new Error(\n      'cy.session from a component spec is not allowed',\n    )\n  })\n\n  Cypress.Commands.overwrite('origin', () => {\n    throw new Error(\n      'cy.origin from a component spec is not allowed',\n    )\n  })\n\n  // @ts-ignore\n  Cypress.on('test:before:after:run:async', () => {\n    optionalCallback?.()\n  })\n}\n","sourceCodeStart":22,"sourceCodeEnd":56,"githubUrl":"https://github.com/cypress-io/cypress/blob/0d85fdc91230885bca0a91df278312800a48b727/npm/mount-utils/src/index.ts#L22-L56","documentation":"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.","triggerScenarios":"Calling `cy.session(id, setup)` inside a component spec. Fires whenever setupHooks has registered the CT overwrites (always, for testingType='component').","commonSituations":"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.","solutions":["Do not use cy.session in component specs; set the state the component needs directly via props or cy.window() mutations.","Stub auth/session dependencies at the component level (e.g. mock a useAuth hook) rather than via cy.session.","Relocate any cy.session logic to e2e specs where a real origin is visited.","Gate shared session helpers behind a testingType check."],"exampleFix":"// before (component spec)\nbeforeEach(() => {\n  cy.session('user', () => { cy.window().then(w => w.localStorage.setItem('token', 'abc')) })\n})\n// after\nbeforeEach(() => {\n  cy.window().then(w => w.localStorage.setItem('token', 'abc'))\n})","handlingStrategy":"type-guard","validationCode":"if (Cypress.testingType === 'e2e') {\n  cy.session(id, setup)\n}","typeGuard":"const isE2E = (): boolean => Cypress.testingType === 'e2e'","tryCatchPattern":null,"preventionTips":["Confine cy.session usage to e2e specs.","In CT, set the state the component needs directly via cy.window or props.","Lint against cy.session in component spec files."],"tags":["component-testing","mount-utils","forbidden-command","cy-session"],"backgroundTag":null,"analyzedSha":"0d85fdc91230885bca0a91df278312800a48b727","analyzedAt":"2026-08-12T16:24:28.056Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}