Skip to content

Error with EventSource undefined #6

@Tryliom

Description

@Tryliom

Hello, I have created an EventSource in a file configJSDom.js:

import EventSource from 'eventsourcemock';

Object.defineProperty(window, 'EventSource', {
  value: EventSource,
});

It was used by jest and in my test I add import { sources } from 'eventsourcemock'; and try to mock that:
new EventSource(Utils.apiUrl()+"/sse_stream", { withCredentials: true });

With that:

sources[Utils.apiUrl()+"/sse_stream"].emitOpen();
  sources['http://example.com/events'].emit(
    messageEvent.type,
    messageEvent
  );

But it crash with that:

    TypeError: Cannot read property 'emitOpen' of undefined

      138 |   tree.instance().startEvent();
      139 |   expect(cookie.get("sse_loading")).toBe("true");
    > 140 |   sources[Utils.apiUrl()+"/sse_stream"].emitOpen();
          |                                         ^
      141 |   sources['http://example.com/events'].emit(
      142 |     messageEvent.type,
      143 |     messageEvent

      at Object.emitOpen (src/__test__/Panel.test.js:140:41)

Have I forgot something ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions