title: Create fake message for EventSource in cypress
body: <p>I am trying to mock SSEs in cypress.  There are a few questions on SO, and a few issues on GitHub about this, but nothing definitive, or particularly relevant to my setup.  I feel like this <em>could</em> be simple, if I can &quot;fake&quot; a message on an EventSource.</p>
  <h2>My Setup</h2>
  <p>I have an <code>EventSource</code> that connects to an SSE endpoint on our server:</p>
  <pre class="lang-js prettyprint-override"><code>const eventSrc = new EventSource(`/api/sseEndpoint`, options);
  </code></pre>
  <p>This gets fed to a subscription function within a redux-saga:</p>
  <pre class="lang-js prettyprint-override"><code>const chan = yield call(subscribe, eventSrc);
  
  const message = yield take(chan)
  
  // pass message to further action/reducer processing:
  yield handleMessage(message) 
  </code></pre>
  <p>Where the subscribe funcrion is pretty simple:</p>
  <pre><code>export function subscribe(eventSrc: EventSource): EventChannel&lt;unknown&gt; {
    const subscriptions: Subscribe&lt;unknown&gt; = emitter =&gt; {
      eventSrc.onmessage = (msg): void =&gt; {
        emitter(msg);
      };
      eventSrc.onerror = fn,
      eventSrc.onopen = fn;
      return (): void =&gt; {
        eventSrc.close();
      };
    };
    return eventChannel(subscriptions);
  }
  </code></pre>
  <p>Where <code>eventChannel</code> is part of the <a href="https://redux-saga.js.org/docs/advanced/Channels/" rel="nofollow noreferrer">redux-saga channel api</a>.</p>
  <p>In need to be able to spoof a message to this EventSource from within cypress.  I am trying to figure out what I can attach to the cypress window in the following fashion:</p>
  <pre class="lang-js prettyprint-override"><code>if (window.Cypress) {
    window.eventSrc = eventSrc;
  }
  </code></pre>
  <p>Such that from within cypress commands, I can create a command to hijack that <code>window.eventSrc</code> to dispatch a message:</p>
  <pre class="lang-js prettyprint-override"><code>Cypress.Commands.add(&quot;sendSSE&quot;, sse =&gt; {
    cy.window()
      .its(&quot;eventSrc&quot;)
      .then(eventSrc =&gt; {
        eventSrc.dispatchEvent({
          data: JSON.stringify(sse),
          lastEventId: &quot;&quot;,
          origin: &quot;http://localhost:3000&quot;,
          type: &quot;message&quot;
        });
      });
  });
  </code></pre>
  <h2>Dispatching / spoofing an event?</h2>
  <p>As you can see, I've tried using the <code>dispatchEvent</code> function which is on the prototype of an <code>EventSource</code>. Unfortunately, I get the error message from within the cypress test:</p>
  <pre><code>Saga or channel was provided with an undefined action
  Hints:
    - check that your Action Creator returns a non-undefined value
    - if the Saga was started using runSaga, check that your subscribe source provides the action to its listeners
  </code></pre>
  <p>I must not be undertanding the <code>dispatchEvent</code> function. Is there a way to use the <code>EventSource</code> that I've attached to the window object to <em>send</em> an event programmatically from within cypress, such that the <code>eventSrc.onMessage = (msg) =&gt; { ... }</code> function will fire?</p>