Cypress: Support for EventSource

Created on 8 Nov 2018  路  8Comments  路  Source: cypress-io/cypress

I would like to see the EventSource support because we use it extensively in our app.

Current behavior:

cy.route doesn't identify EventSource requests and there is no way to watch them reliably now.

Desired behavior:

Either cy.route should support it or we should have another API for EventSource.

proposal 馃挕 network feature

Most helpful comment

I've come up with a solution to mock EventSources in a Cypress test:
You can use a package similar to https://github.com/gcedo/eventsourcemock

In your main file of your application, you can then add:

import EventSource, { sources聽} from 'eventsourcemock';

if (window['Cypress']) {
  // Add mocked EventSources to Cypress run
  Object.defineProperty(window, 'EventSource', { value: EventSource });
  Object.defineProperty(window, 'mockEventSources', { value: sources });
}

And in your test, you can use:

cy.window().its('mockEventSources' as any).then(mockEventSources => {
  cy.readFile('cypress/fixtures/<your fixture here>').then(fixture => {
    mockEventSources['<your name here>'].emit(
      messageEvent.type,
      messageEvent
    );
  });
});

I hope this helps some people.

EDIT If you don't want to add to your application's main file, you can also use the solution from #2349

All 8 comments

Also, is there any workaround now?

Same problem here:)

Hey @buremba & @sashashakun, is the cy.route() not working during cypress open, cypress run, or both?

What is the current behavior of Cypress when trying to use cy.route() with EventSource? Does it error? Just not match the route?

@jennifer-shehane
I think it does not match the route.

The console states: GET http://localhost:3000/sse-stream/HL01 net::ERR_EMPTY_RESPONSE
That is the path for my EventStream.
The cy.route configuration is:

cy.route(
      'GET',
      `sse-stream/HL01`,
      'fixture:load-carrier-arrived.json'
    );

Update
based on some digging around I found that EventSource can't accept custom headers.
I've noticed that cypress sets request headers on XHR, but not on EventSource.
I even wonder if it's actually possible. A stream is not the same as a simple XHR.
Would there be a way to override the EventSource with some JavaScript stream?

I've come up with a solution to mock EventSources in a Cypress test:
You can use a package similar to https://github.com/gcedo/eventsourcemock

In your main file of your application, you can then add:

import EventSource, { sources聽} from 'eventsourcemock';

if (window['Cypress']) {
  // Add mocked EventSources to Cypress run
  Object.defineProperty(window, 'EventSource', { value: EventSource });
  Object.defineProperty(window, 'mockEventSources', { value: sources });
}

And in your test, you can use:

cy.window().its('mockEventSources' as any).then(mockEventSources => {
  cy.readFile('cypress/fixtures/<your fixture here>').then(fixture => {
    mockEventSources['<your name here>'].emit(
      messageEvent.type,
      messageEvent
    );
  });
});

I hope this helps some people.

EDIT If you don't want to add to your application's main file, you can also use the solution from #2349

Hi.
As I understood from #2054 support for EventSource got to be already done?
But for me it does not work https://github.com/cypress-io/cypress/pull/2054#issuecomment-627225977

I can not use any "mock" things because it is real server requests and I need data from it.

Hey @buremba & @sashashakun, is the cy.route() not working during cypress open, cypress run, or both?

What is the current behavior of Cypress when trying to use cy.route() with EventSource? Does it error? Just not match the route?

When running my tests, waiting for an sse, the wait never finishes successfully:
cy.route times out for me with "No request ever occurred."
cy.route2 times out for me with "No response ever occurred."

As @drumslave-git mentioned, i too need access to the response/events or at least a way to tell when the sse has received the first event to continue. Hope this information helps, feel free to reach out for further informations or advice on how to proceed with this.

Thanks,
Marcel

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zbigniewkalinowski picture zbigniewkalinowski  路  3Comments

jennifer-shehane picture jennifer-shehane  路  3Comments

EirikBirkeland picture EirikBirkeland  路  3Comments

tahayk picture tahayk  路  3Comments

brian-mann picture brian-mann  路  3Comments