Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
osdiab opened this issue Apr 14, 2023 · 34 comments
Closed

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

osdiab opened this issue Apr 14, 2023 · 34 comments
Labels
Reproducible Can be reproduced stale no activity on this issue for a long period type: bug

Comments

@osdiab
Copy link

osdiab commented Apr 14, 2023

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
Copy link
Contributor

mirobo commented Apr 18, 2023

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
Copy link
Contributor

@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
Copy link
Author

osdiab commented Apr 19, 2023

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
Copy link
Member

@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
Copy link

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
Copy link
Contributor

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
Copy link

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
Copy link
Contributor

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

@blessanm86
Copy link

@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
Copy link

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

@blessanm86
Copy link

blessanm86 commented May 2, 2023

@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
Copy link

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
Copy link
Contributor

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 [email protected] if you are able to share something privately

@MDanialSaleem
Copy link

MDanialSaleem commented May 18, 2023

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
Copy link
Contributor

@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
Copy link

@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
Copy link
Contributor

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

@marktnoonan
Copy link
Contributor

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 marktnoonan added CT Issue related to component testing E2E Issue related to end-to-end testing Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. labels May 19, 2023
@marktnoonan
Copy link
Contributor

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

@warrensplayer
Copy link
Contributor

@marktnoonan
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

Estimating based on adding the check

@AlessandroCaggiano
Copy link

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
Copy link
Contributor

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

@AlessandroCaggiano
Copy link

  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
Copy link
Contributor

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
Copy link

Sorry, I can't share this repository.

@beshanoe
Copy link

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
Copy link

calffey commented Sep 8, 2023

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

@MulleDev
Copy link

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
Copy link

shaheryarshakeel commented Dec 18, 2023

Hi All,

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

Please respond asap, thanks..

@jennifer-shehane jennifer-shehane added type: bug Reproducible Can be reproduced and removed Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. labels Jan 2, 2024
@jennifer-shehane jennifer-shehane removed CT Issue related to component testing E2E Issue related to end-to-end testing labels May 15, 2024
@cypress-app-bot
Copy link
Collaborator

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.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label Nov 12, 2024
@cypress-app-bot
Copy link
Collaborator

This issue has been closed due to inactivity.

@cypress-app-bot cypress-app-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Reproducible Can be reproduced stale no activity on this issue for a long period type: bug
Projects
None yet
Development

No branches or pull requests