cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.04k stars 3.18k forks source link

Cannot read properties of undefined (reading 'cookies') #26498

Open osdiab opened 1 year ago

osdiab commented 1 year ago

Current behavior

Not sure why, but every now and again multiple members of our team get the error above in the Cypress test window along with this stack trace:

TypeError: Cannot read properties of undefined (reading 'cookies')
    at <embedded>:4613:284999
From previous event:
    at $Cypress.automation (http://e2e.localhost:4000/__cypress/runner/cypress_runner.js:147779:12)
    at Object.getCookies (http://e2e.localhost:4000/__cypress/runner/cypress_runner.js:140904:29)
    at Object.clearCookies (http://e2e.localhost:4000/__cypress/runner/cypress_runner.js:140910:77)
    at Object.clearCurrentSessionData (http://e2e.localhost:4000/__cypress/runner/cypress_runner.js:140901:127)
    at http://e2e.localhost:4000/__cypress/runner/cypress_runner.js:140354:110

looks like something in the cypress runner that clears session between tests is failing.

Desired behavior

No response

Test code to reproduce

it's inconsistent when it happens, so it's hard for me to make a clear repro. But here is our cypress config:

import { defineConfig } from "cypress";
import { gql } from "@apollo/client";

import { makeAdminApolloClient } from "./src/utility/admin-gql-client";
import { readPdf } from "./src/utility/read-pdf";

export default defineConfig({
  hosts: {
    // normally, cypress doesn't work with subdomains of localhost; this makes
    // it work
    // https://github.com/cypress-io/cypress/issues/1488#issuecomment-396435553
    "*.localhost": "127.0.0.1",
  },
  e2e: {
    projectId: process.env.CYPRESS_PROJECT_ID,
    baseUrl: process.env.CYPRESS_BASE_URL,
    fileServerFolder: ".",
    fixturesFolder: "./src/fixtures",
    specPattern: "./src/integration/**/*.cy.{js,jsx,ts,tsx}",
    modifyObstructiveCode: false,
    supportFile: "./src/support/e2e.ts",
    video: true,
    screenshotsFolder: "./screenshots",
    videosFolder: "./videos",
    chromeWebSecurity: false, // allows to go cross-origin
    retries: 3,
    defaultCommandTimeout: 10_000,
});

Cypress Version

12.9.0

Node version

16.16.0

Operating System

macOS 13.2.1

Debug Logs

No response

Other

No response

mirobo commented 1 year ago

Same issue with Cypress 12.9.0, Node: 18.13.0, Windows 10.0.19044, Built-in Electron. Reproduction is also inconsistent but when it happens, it involves cy.session().

jordanpowell88 commented 1 year ago

@mirobo, @osdiab We would love to take a look at this issue. I know you said it's inconsistent and hard to reproduce but can either of you provide a reproducible example so we can investigate further?

osdiab commented 1 year ago

I'll ask my colleagues to keep an eye out for when it happens to them and hopefully they can record a video or something of it happening, hopefully from that can divine the cause and some repro steps a little better.

emilyrohrbough commented 1 year ago

@jordanpowell88 Following the code - this is likely coming from here where we are parsing the cookies from the response...

It would be interesting to understand typically when this error is hit since, to better account for what might be going on to see this error, but we likely need a safe-guard here as well to ensure the response is defined.

albinohrn commented 1 year ago

We see this error occasionally when running our component tests in CI (Jenkins).

Cannot read properties of undefined (reading 'cookies')  Because this error occurred during a `before all` hook we are skipping all of the remaining tests.
TypeError: Cannot read properties of undefined (reading 'cookies')

Because this error occurred during a `before all` hook we are skipping all of the remaining tests.
    at <embedded>:4649:284999
    at process.processTicksAndRejections (node:internal/process/task_queues:96:5)
jordanpowell88 commented 1 year ago

It would be interesting to understand typically when this error is hit since, to better account for what might be going on to see this error, but we likely need a safe-guard here as well to ensure the response is defined.

That certainly looks like the LOC @emilyrohrbough . Is anyone able to provide a reproducible example?

blessanm86 commented 1 year ago

See the same issue popup intermittently in our component tests as @albinohrn mentioned. It happens in the beforeEach block and we do nothing there with cookies. So not sure what is causing the issue.

Browserslist: caniuse-lite is outdated. Please run:
  npx browserslist@latest --update-db
  Why you should do it regularly: https://github.com/browserslist/browserslist#browsers-data-updating
Timed out waiting for the browser to connect. Retrying...
<i> [webpack-dev-middleware] wait until bundle finished: /__cypress/src/index.html
73 assets
1892 modules
webpack 5.48.0 compiled successfully in 115523 ms
No errors found.
  Employee Notes
    (Attempt 1 of 2) render loading state
    1) "before each" hook for "render loading state"
  0 passing (713ms)
  1 failing
  1) Employee Notes
       "before each" hook for "render loading state":
     TypeError: Cannot read properties of undefined (reading 'cookies')
Because this error occurred during a `before each` hook we are skipping the remaining tests in the current suite: `Employee Notes`
      at <embedded>:4649:284999
      at process.processTicksAndRejections (node:internal/process/task_queues:96:5)

The test itself is barely doing anything

import { EmployeeNotes } from '../../pages/EmployeeNotes/EmployeeNotes';

describe('Employee Notes', () => {
  beforeEach(() => {
    cy.clock(Date.UTC(2021, 2, 1), ['Date']);
  });

  it('render loading state', () => {
    cy.mount(<EmployeeNotes />);

    cy.findByTestId('employee-notes-loading-state').should('exist');
  });

  it('render notes', () => {
    cy.interceptGetEmployeeNotesAPI();

    cy.mount(<EmployeeNotes />);

    cy.wait(['@getEmployeeNotes']);

    cy.findByTestId('employee-notes-container').should('exist');
  });
});
warrensplayer commented 1 year ago

@blessanm86 Do you have a beforeEach in your component test support file?

blessanm86 commented 1 year ago

@warrensplayer No I don't. It's just a bunch of commands and interceptors. But I do import the following in there. Not sure if they do something.

import '@testing-library/cypress/add-commands';
import { configure } from '@testing-library/cypress';
werge2121 commented 1 year ago

@warrensplayer We have the same issue and we do have a global beforeEach that sets some fixture data.

blessanm86 commented 1 year ago

@warrensplayer So I tried enabling cypress logs and take a dump. Here is a link to the gist. I hope this can shed some light

andrewfhw commented 1 year ago

I just experienced this issue as well. I'm also not doing anything with sessions or cookies. I restarted Cypress and the error disappeared.

marktnoonan commented 1 year ago

For folks who experience this pretty consistently, if you change your e2e config to us testIsolation: false does that prevent the issue from happening? It might have other side effects so I wouldn't recommend just setting that as a solution, but I wonder if something is happening in Cypress's own cleanup in-between specs, and this would point us in the right direction.

I'm also curious if there is a publicly-accessible URL I could test with, feel free to email me at mark@cypress.io if you are able to share something privately

MDanialSaleem commented 1 year ago

This happens with us in gitlab ci as well, curiously it's always one particular test that runs into this error. This is the image we are using to test: cypress/browsers:node14.17.6-chrome100-ff98. Attempts to reproduce this locally have failed. Cypress version: 12.11.0

Edit: we are not using any beforeeach.

marktnoonan commented 1 year ago

@MDanialSaleem I'm curious with the testIsolation: false configuration does that problem still occur in that specific test in CI?

Attempts to reproduce this locally have failed.

This must be frustrating. Any more details about what that specific test might be doing that others are not?

MDanialSaleem commented 1 year ago

@MDanialSaleem I'm curious with the testIsolation: false configuration does that problem still occur in that specific test in CI?

Attempts to reproduce this locally have failed.

This must be frustrating. Any more details about what that specific test might be doing that others are not?

We don't yet have e2e tests with cypress yet so I can't enable testIsolation. These are component tests (we're migrating over from jest, one of the prime reasons was the flakiness of jsdom based tests so it's kinda funny that's this is happening lol).

To answer your second question, no, all tests are pretty much the same, nothing special about this one. I'll try and create a reproducable example without any of our proprietary code if this issue persists. And if we're able to figure out a workaround/root cause, I'll update here as well.

marktnoonan commented 1 year ago

Thanks! Even the information that this is a component test helps a lot as it rules out a lot of things.

marktnoonan commented 1 year ago

After debugging a little bit, I think I have found at least where this is triggered, even if not the underlying cause:

https://github.com/cypress-io/cypress/blob/develop/packages/server/lib/browsers/cdp_automation.ts#L225

If result is undefined in return normalizeGetCookies(result.cookies), I get this stack trace that matches what has been shown in this issue.

So for when this is picked up: it seems like this.sendDebuggerCommandFn('Network.getAllCookies') must sometimes be able to resolve with undefined as the result? I'm not familiar with this part of the code or exactly how this all works.

Stacktrace from forcing this to happen on develop. Notice the last 5 lines.

TypeError: Cannot read properties of undefined (reading 'cookies')
    at /path/to/cypress/packages/server/lib/browsers/cdp_automation.ts:227:40
    at /path/to/cypress/packages/server/lib/automation/automation.ts:55:25
    at Cookies.getCookies (/path/to/cypress/packages/server/lib/automation/cookies.ts:116:12)
    at /path/to/cypress/packages/server/lib/automation/automation.ts:106:31
    at Automation.normalize (/path/to/cypress/packages/server/lib/automation/automation.ts:101:25)
    at /path/to/cypress/packages/server/lib/automation/automation.ts:170:19
    at processImmediate (node:internal/timers:466:22)
    at Automation.request (/path/to/cypress/packages/server/lib/automation/automation.ts:169:6)
    at automationRequest (/path/to/cypress/packages/server/lib/socket-base.ts:166:25)
    at Socket.<anonymous> (/path/to/cypress/packages/server/lib/socket-base.ts:265:16)
    at Socket.emit (node:events:527:29)
    at Socket.emitUntyped (<embedded>:852852:21)
    at <embedded>:853114:32
    at processTicksAndRejections (node:internal/process/task_queues:78:12)
From previous event:
    at $Cypress.automation (cypress:///../driver/src/cypress.ts:630:12)
    at Object.getCookies (cypress:///../driver/src/cy/commands/sessions/manager.ts:121:29)
    at Object.clearCookies (cypress:///../driver/src/cy/commands/sessions/manager.ts:127:77)
    at Object.clearCurrentSessionData (cypress:///../driver/src/cy/commands/sessions/manager.ts:118:127)
    at eval (cypress:///../driver/src/cy/commands/sessions/index.ts:41:110)
marktnoonan commented 1 year ago

Note: routed this to both E2E and CT teams, since it affects both.

warrensplayer commented 1 year ago

Hey team! Please add your planning poker estimate with Zenhub @astone123 @dkasper-was-taken @jordanpowell88 @lmiller1990 @marktnoonan @mike-plummer

marktnoonan commented 1 year ago

I'm estimating based on a path forward where we add a check for undefined and retry sendDebuggerCommandFn until some timeout, then throw the error, since it's not something that makes sense to ignore if undefined is found. Assuming the underlying cause is the CDP just dropping this.

lmiller1990 commented 1 year ago

I am estimating this based on the fact there is no reliable, minimal reproduction, meaning it will be hard to both debug, fix and test with confidence we are fixing the actual, underlying bug.

warrensplayer commented 1 year ago

Estimating based on adding the check

AlessandroCaggiano commented 1 year ago

I am facing this issue too, at the moment. For me, this issue occurs every time after I opened the URL from our Website in the Cypress GUI and closed it again.

lmiller1990 commented 1 year ago

Can you provide a minimal reproduction @AlessandroCaggiano ? Is it consistent?

AlessandroCaggiano commented 1 year ago
  1. I open cypress with yarn in the terminal (yarn cypress-open)
  2. I choose some random test
  3. I run the test
  4. After the test has either succeeded or failed I click on the URL in the top of the screen. Btw I'm using Electron. Maybe on chrome you can't do that. I never tested on chrome.
  5. I close the window with the opened URL
  6. I restart the same test
  7. 'Cannot read properties of undefined (reading 'cookies')' appears

It's a consistent error

lmiller1990 commented 1 year ago

Can you share some random test? Or, ideally, a minimal reproduction - if you can share a repository we can clone and use to reproduce the error consistently, that would make fixing this a whole lot easier.

Right now it's hard to be confident in any patch since we cannot consistently reproduce the error, but it sounds like you are able to do so.

AlessandroCaggiano commented 1 year ago

Sorry, I can't share this repository.

beshanoe commented 1 year ago

In my case I got this error due to this code

cy.window().then(win => {
      cy.stub(win, 'open').as('windowOpen');
    });

called before the page actually loads after the cy.visit(...).

calffey commented 1 year ago

Still running into this issue. Has anyone implemented a fix or found a solution?

MulleDev commented 1 year ago

Hi all, I iave this problem also. its reproducable. The Error accurs, when a click on the UI opens a pdf file in a new tab. I tried to prevent it with .invoke('removeAttr', 'target', "_self") before clicking, but n the tests is always a new empty window opened. The Tests goes on but when restart the test later, this error accurs.

is there anybody on that?

shaheryarshakeel commented 11 months ago

Hi All,

Did someone already fixed this issue in some other PR or may be I can work on that?

Please respond asap, thanks..

cypress-app-bot commented 2 days ago

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.