cypress-io / cypress

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

Unexpected token in JSON #22997

Closed alexplischke closed 1 year ago

alexplischke commented 2 years ago

Current behavior

Occasionally hitting this issue when running on MacOS 12, Node v14.20.0 and Chrome 103 / Firefox 103.

================================================================================

  (Run Starting)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Cypress:        10.3.1                                                                         │
  │ Browser:        Custom Chrome 103                                                              │
  │ Node Version:   v14.20.0 (/Users/chef/payload/bundle/bundle/node)                              │
  │ Specs:          20 found (todo.cy.js, actions.cy.js, aliasing.cy.js, assertions.cy.js, connect │
  │                 ors.cy.js, cookies.cy.js, cypress_api.cy.js, files.cy.js, local_storage.cy.js, │
  │                  location.cy.js, misc.cy.js, navigation.cy.js, network_requests.cy.js, queryin │
  │                 g.cy.js, spies_stubs_cl...)                                                    │
  │ Searched:       **/*cy.js                                                                      │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘

────────────────────────────────────────────────────────────────────────────────────────────────────

  Running:  todo.cy.js                                                                     (1 of 20)
Unexpected token < in JSON at position 0
SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>:null:null)
    at /Users/chef/payload/bundle/bundle/Cache/10.3.1/Cypress.app/Contents/Resources/app/node_modules/chrome-remote-interface/lib/devtools.js:121:33
    at IncomingMessage.<anonymous> (/Users/chef/payload/bundle/bundle/Cache/10.3.1/Cypress.app/Contents/Resources/app/node_modules/chrome-remote-interface/lib/external-request.js:32:17)
    at IncomingMessage.emit (node:events:402:35)
    at IncomingMessage.emit (node:domain:475:12)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Desired behavior

Error should not occur.

Test code to reproduce

Cypress kitchensink example.

Cypress Version

10.3.1

Other

No response

rockindahizzy commented 2 years ago

Hey @alexplischke, thanks for opening this issue! I was not able to reproduce this using the kitchen sink example. Is there any additional information you can provide that might help pinpoint the cause of the issue?

alexplischke commented 2 years ago

Thank you for taking a look!

I unfortunately do not have any additional information on this. I suppose it would have been helpful if chrome-remote-interface would print the response body in plain text if it was not able to parse the JSON response from the browser.

rockindahizzy commented 2 years ago

Hey @alexplischke,

There are few different logs that might provide useful information. Unfortunately, I can't reproduce this. I would greatly appreciate it if you had the time to try to grab the logs in the failure scenario.

DEBUG=cypress:server:browsers:cri-client*,cypress-verbose:server:browsers:cri-client*,cypress:server:browsers:browser-cri-client npx cypress run

alexplischke commented 2 years ago

I ran with the DEBUG setting that was provided, but was not able to get any more information out of this error case:

================================================================================

  (Run Starting)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Cypress:        10.3.1                                                                         │
  │ Browser:        Custom Firefox 103                                                             │
  │ Node Version:   v14.20.0 (/Users/chef/payload/bundle/bundle/node)                              │
  │ Specs:          3 found (misc.cy.js, cookies.cy.js, aliasing.cy.js)                            │
  │ Searched:       cypress/e2e/2-advanced-examples/misc.cy.js, cypress/e2e/2-advanced-examples/co │
  │                 okies.cy.js, cypress/e2e/2-advanced-examples/aliasing.cy.js                    │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘

────────────────────────────────────────────────────────────────────────────────────────────────────

  Running:  misc.cy.js                                                                      (1 of 3)
Unexpected token < in JSON at position 0
SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>:null:null)
    at /Users/chef/payload/bundle/bundle/Cache/10.3.1/Cypress.app/Contents/Resources/app/node_modules/chrome-remote-interface/lib/devtools.js:121:33
    at IncomingMessage.<anonymous> (/Users/chef/payload/bundle/bundle/Cache/10.3.1/Cypress.app/Contents/Resources/app/node_modules/chrome-remote-interface/lib/external-request.js:32:17)
    at IncomingMessage.emit (node:events:402:35)
    at IncomingMessage.emit (node:domain:475:12)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

For what it's worth, I was able to reproduce another connection issue that we intermittently run into. For this one, the browser does indeed start, but remains blank (cypress doesn't load itself into it).

================================================================================

  (Run Starting)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Cypress:        10.3.1                                                                         │
  │ Browser:        Custom Firefox 103                                                             │
  │ Node Version:   v14.20.0 (/Users/chef/payload/bundle/bundle/node)                              │
  │ Specs:          3 found (misc.cy.js, cookies.cy.js, aliasing.cy.js)                            │
  │ Searched:       cypress/e2e/2-advanced-examples/misc.cy.js, cypress/e2e/2-advanced-examples/co │
  │                 okies.cy.js, cypress/e2e/2-advanced-examples/aliasing.cy.js                    │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘

────────────────────────────────────────────────────────────────────────────────────────────────────

  Running:  misc.cy.js                                                                      (1 of 3)
2022-08-11T23:09:53.540Z cypress:server:browsers:browser-cri-client error finding browser target, maybe retrying { delay: 100, err: Error: <html>                    <head><title>404 Not Found</title></head>                    <body>                      <h1>404 Not Found</h1>                      <p>                        <span style='font-family: monospace;'>&#47;&#106;&#115;&#111;&#110;&#47;&#118;&#101;&#114;&#115;&#105;&#111;&#110;</span> was not found.                      </p>                    </body>                  </html> at IncomingMessage.<anonymous> (/Users/chef/payload/bundle/bundle/Cache/10.3.1/Cypress.app/Contents/Resources/app/node_modules/chrome-remote-interface/lib/external-request.js:34:26) at IncomingMessage.emit (node:events:402:35) at IncomingMessage.emit (node:domain:475:12) at endReadableNT (node:internal/streams/readable:1343:12) at processTicksAndRejections (node:internal/process/task_queues:83:21)  }
2022-08-11T23:09:54.268Z cypress:server:browsers:browser-cri-client error finding browser target, maybe retrying { delay: 100, err: Error: <html>                    <head><title>404 Not Found</title></head>                    <body>                      <h1>404 Not Found</h1>                      <p>                        <span style='font-family: monospace;'>&#47;&#106;&#115;&#111;&#110;&#47;&#118;&#101;&#114;&#115;&#105;&#111;&#110;</span> was not found.                      </p>                    </body>                  </html> at IncomingMessage.<anonymous> (/Users/chef/payload/bundle/bundle/Cache/10.3.1/Cypress.app/Contents/Resources/app/node_modules/chrome-remote-interface/lib/external-request.js:34:26) at IncomingMessage.emit (node:events:402:35) at IncomingMessage.emit (node:domain:475:12) at endReadableNT (node:internal/streams/readable:1343:12) at processTicksAndRejections (node:internal/process/task_queues:83:21)  }
2022-08-11T23:09:54.732Z cypress:server:browsers:browser-cri-client error finding browser target, maybe retrying { delay: 100, err: Error: <html>                    <head><title>404 Not Found</title></head>                    <body>                      <h1>404 Not Found</h1>                      <p>                        <span style='font-family: monospace;'>&#47;&#106;&#115;&#111;&#110;&#47;&#118;&#101;&#114;&#115;&#105;&#111;&#110;</span> was not found.                      </p>                    </body>                  </html> at IncomingMessage.<anonymous> (/Users/chef/payload/bundle/bundle/Cache/10.3.1/Cypress.app/Contents/Resources/app/node_modules/chrome-remote-interface/lib/external-request.js:34:26) at IncomingMessage.emit (node:events:402:35) at IncomingMessage.emit (node:domain:475:12) at endReadableNT (node:internal/streams/readable:1343:12) at processTicksAndRejections (node:internal/process/task_queues:83:21)  }
2022-08-11T23:09:54.927Z cypress:server:browsers:browser-cri-client error finding browser target, maybe retrying { delay: 100, err: Error: <html>                    <head><title>404 Not Found</title></head>                    <body>                      <h1>404 Not Found</h1>                      <p>                        <span style='font-family: monospace;'>&#47;&#106;&#115;&#111;&#110;&#47;&#118;&#101;&#114;&#115;&#105;&#111;&#110;</span> was not found.                      </p>                    </body>                  </html> at IncomingMessage.<anonymous> (/Users/chef/payload/bundle/bundle/Cache/10.3.1/Cypress.app/Contents/Resources/app/node_modules/chrome-remote-interface/lib/external-request.js:34:26) at IncomingMessage.emit (node:events:402:35) at IncomingMessage.emit (node:domain:475:12) at endReadableNT (node:internal/streams/readable:1343:12) at processTicksAndRejections (node:internal/process/task_queues:83:21)  }
2022-08-11T23:09:55.096Z cypress:server:browsers:browser-cri-client error finding browser target, maybe retrying { delay: 100, err: Error: <html>                    <head><title>404 Not Found</title></head>                    <body>                      <h1>404 Not Found</h1>                      <p>                        <span style='font-family: monospace;'>&#47;&#106;&#115;&#111;&#110;&#47;&#118;&#101;&#114;&#115;&#105;&#111;&#110;</span> was not found.                      </p>                    </body>                  </html> at IncomingMessage.<anonymous> (/Users/chef/payload/bundle/bundle/Cache/10.3.1/Cypress.app/Contents/Resources/app/node_modules/chrome-remote-interface/lib/external-request.js:34:26) at IncomingMessage.emit (node:events:402:35) at IncomingMessage.emit (node:domain:475:12) at endReadableNT (node:internal/streams/readable:1343:12) at processTicksAndRejections (node:internal/process/task_queues:83:21)  }
2022-08-11T23:09:55.333Z cypress:server:browsers:browser-cri-client error finding browser target, maybe retrying { delay: 100, err: Error: <html>                    <head><title>404 Not Found</title></head>                    <body>                      <h1>404 Not Found</h1>                      <p>                        <span style='font-family: monospace;'>&#47;&#106;&#115;&#111;&#110;&#47;&#118;&#101;&#114;&#115;&#105;&#111;&#110;</span> was not found.                      </p>                    </body>                  </html> at IncomingMessage.<anonymous> (/Users/chef/payload/bundle/bundle/Cache/10.3.1/Cypress.app/Contents/Resources/app/node_modules/chrome-remote-interface/lib/external-request.js:34:26) at IncomingMessage.emit (node:events:402:35) at IncomingMessage.emit (node:domain:475:12) at endReadableNT (node:internal/streams/readable:1343:12) at processTicksAndRejections (node:internal/process/task_queues:83:21)  }
2022-08-11T23:09:55.878Z cypress:server:browsers:browser-cri-client error finding browser target, maybe retrying { delay: 100, err: Error: <html>                    <head><title>404 Not Found</title></head>                    <body>                      <h1>404 Not Found</h1>                      <p>                        <span style='font-family: monospace;'>&#47;&#106;&#115;&#111;&#110;&#47;&#118;&#101;&#114;&#115;&#105;&#111;&#110;</span> was not found.                      </p>                    </body>                  </html> at IncomingMessage.<anonymous> (/Users/chef/payload/bundle/bundle/Cache/10.3.1/Cypress.app/Contents/Resources/app/node_modules/chrome-remote-interface/lib/external-request.js:34:26) at IncomingMessage.emit (node:events:402:35) at IncomingMessage.emit (node:domain:475:12) at endReadableNT (node:internal/streams/readable:1343:12) at processTicksAndRejections (node:internal/process/task_queues:83:21)  }
2022-08-11T23:09:55.988Z cypress:server:browsers:cri-client connecting { target: 'ws://localhost:49219/devtools/browser/d470e69a-12b0-48f0-ae3b-3ca4e078f7f9' }
2022-08-11T23:09:56.025Z cypress:server:browsers:browser-cri-client Attaching to target url about:blank
2022-08-11T23:09:56.026Z cypress-verbose:server:browsers:cri-client:send:[-->] sending CDP command { id: 1, method: 'Target.getTargets', params: {} }
2022-08-11T23:09:56.034Z cypress-verbose:server:browsers:cri-client:recv:[<--] received CDP message { id: 1, result: { targetInfos: [ [Object] ] } }
2022-08-11T23:09:56.034Z cypress:server:browsers:cri-client connecting { target: 'aaaa1c17-7f74-4aa5-beed-a7e73d339c00' }
2022-08-11T23:09:56.058Z cypress:server:browsers:cri-client registering CDP on event { eventName: 'Network.requestWillBeSent' }
2022-08-11T23:09:56.058Z cypress:server:browsers:cri-client registering CDP on event { eventName: 'Network.responseReceived' }
2022-08-11T23:09:56.058Z cypress-verbose:server:browsers:cri-client:send:[-->] sending CDP command { id: 1, method: 'Network.enable', params: { maxTotalBufferSize: 0, maxResourceBufferSize: 0, maxPostDataSize: 0 } }
2022-08-11T23:09:56.066Z cypress-verbose:server:browsers:cri-client:recv:[<--] received CDP message { id: 1, result: {} }

Timed out waiting for the browser to connect. Retrying...
Still waiting to connect to Firefox, retrying in 1 second (attempt 18/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 19/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 20/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 21/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 22/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 23/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 24/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 25/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 26/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 27/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 28/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 29/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 30/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 31/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 32/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 33/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 34/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 35/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 36/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 37/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 38/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 39/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 40/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 41/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 42/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 43/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 44/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 45/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 46/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 47/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 48/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 49/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 50/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 51/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 52/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 53/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 54/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 55/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 56/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 57/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 58/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 59/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 60/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 61/62)
Still waiting to connect to Firefox, retrying in 1 second (attempt 62/62)
2022-08-11T23:11:41.293Z cypress:server:browsers:browser-cri-client failed to connect to CDP { connectOpts: { host: '127.0.0.1', port: 49302, getDelayMsForRetry: [Function: getDelayMsForRetry] }, err: OperationalError: connect ECONNREFUSED 127.0.0.1:49302 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) { cause: Error: connect ECONNREFUSED 127.0.0.1:49302 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) { errno: -61, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 49302 }, isOperational: true, errno: -61, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 49302 } }
Cypress failed to make a connection to Firefox.

This usually indicates there was a problem opening the Firefox browser.

Error: connect ECONNREFUSED 127.0.0.1:49303
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)
rockindahizzy commented 2 years ago

@alexplischke thanks for providing more details. I wonder if you are having network blips while running the tests. Are you running the kitchen sink app locally when running the tests?

alexplischke commented 2 years ago

The tests are actually targeting the cypress hosted examples (https://example.cypress.io/).

mike-plummer commented 2 years ago

@alexplischke We're still struggling to reproduce this on our end. I've run "cypress-example-kitchensink" against example.cypress.io a dozen times without issue, so there must be something unique to your environment or test setup that we haven't thought of.

Thinking through things that might be causing this issue for you but not for us, is it possible you're on a corporate network/VPN that might be interjecting some network middleware (proxy, etc)? My suspicion is that the "<" unexpected character is the beginning of an HTML response from a network appliance that stomped on a JSON request.

alexplischke commented 2 years ago

it possible you're on a corporate network/VPN

Yep!

might be interjecting some network middleware (proxy, etc)? My suspicion is that the "<" unexpected character is the beginning of an HTML response from a network appliance that stomped on a JSON request.

But isn't the connection completely localized between the cypress instance and the browser?

mike-plummer commented 2 years ago

@alexplischke Yes, you're correct, the chrome-remote-interface connection in this instance should be local, but it's all IP-based so while we would hope a request to 127.0.0.1:12345 would stay local there is the possibility of VPN software or custom hosts rules grabbing that traffic. I would hope not, but I've seen weirder stuff happen

So, where do we go from here? We haven't been able to reproduce this locally and you've told us there isn't anything of note in the debug logs which makes this a tough nut to crack. Is there anything else unique about your environment that might help narrow down the issue? A couple questions off the top of my head:

  1. Do you still see this issue when running off your corporate VPN/network?
  2. I notice the userpath in the error is /Users/chef - any chance you're using a product like Chef which may be imposing some network routing/rules of its own?
  3. I see the errors are reporting "Custom Chrome 103" and "Custom Firefox 103" - I presume that indicates custom-built binaries? Do you see this issue against off-the-shelf browser versions?
alexplischke commented 2 years ago

Do you still see this issue when running off your corporate VPN/network?

Unfortunately I can't control that in the environment where I'm experiencing this issue.

I notice the userpath in the error is /Users/chef - any chance you're using a product like Chef which may be imposing some network routing/rules of its own?

That's just our internal service name, unrelated to the Chef product 😅

I see the errors are reporting "Custom Chrome 103" and "Custom Firefox 103" - I presume that indicates custom-built binaries? Do you see this issue against off-the-shelf browser versions?

Something worth mentioning is that I don't get those kind of connection errors if I use playwright or puppeteer.

Right now I'm trying to replicate what cypress is doing under the hood by using chrome-remote-interface directly. I'll keep you posted if I can replicate it this way 🤞

alexplischke commented 2 years ago

Replicated 🥵

There is a proxy service that starts on 49221 and 55001. If that overlapped with the random port that Cypress picked, it resulted in the JSON error above.

Thanks a lot for the help and sorry for any inconvenience 🙏

PS: Still baffling why cypress (or more specifically the get-port lib wanted to pick a port that was already occupied). Maybe the newer release of get-port is able to address this? Has the following change: https://github.com/sindresorhus/get-port/commit/c3bbed9545ce749331b32e40ae59712266e84c1b

alexplischke commented 2 years ago

Indeed, I'm not running into this issue after updating get-port to version 6.1.2. Would be great if that could find its way into cypress 🙏

mike-plummer commented 2 years ago

@alexplischke Wow, amazing detective work! 🤯 It was no inconvenience at all - I'm glad you were able to track down what was going on, sorry I wasn't able to be more helpful.

It sounds like you have a workaround (or at least a "oh, that's why that's happening"), but I agree that what you were seeing should not be happening. I suspect you're right about the change you identified in get-port being the magic sauce - you must have multiple network interfaces on your system and the chosen ports were already reserved but on the non-primary, or something like that. I'm going to forward your findings on to the team to see about upgrading get-port in a future release - the only potential issue I see is that get-port v6 requires Node 12.20 which is slightly higher than our current required Node baseline (v12.x)

nagash77 commented 2 years ago

Reopening so we can use this ticket to track the status of updating the get-port library once we have a minimum node version that allows us to require node >12.20. Going to route this ticket to the E2E team for prioritization in their backlog.

alexplischke commented 1 year ago

Could the update of get-port be possible now? Considering that Cypress requires Node 16+ these days.

cypress-app-bot commented 1 year 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.

cypress-app-bot commented 1 year ago

This issue has been closed due to inactivity.