The objective is to put the EventSource-related functionality in a class such as:
export default class EventSourceSetup {
constructor() {
let eventSource = new EventSource('http://localhost');
eventSource.addEventListener('loading', function (event) {
})
eventSource.addEventListener('loaded', function (event) {
})
eventSource.addEventListener('error', function (event) {
})
eventSource.onerror = error => {
console.error('EventSource failed: ', error);
};
}
}
The server should be mocked and the complete client-side functionality with window and EventSource in the browser should be used.
And to test it such as:
import EventSourceSetup from './thumbnails'
describe('SSE', () => {
beforeAll(() => {
});
it('first', () => {
const eventSourceSetup = new EventSourceSetup();
});
});
But when I do this I see the following error:
ReferenceError: EventSource is not defined
at new EventSourceSetup (/Users/pharmosan/IdeaProjects/thumbnails-frontend/thumbnails.js:3:27)
at Object.<anonymous> (/Users/pharmosan/IdeaProjects/thumbnails-frontend/thumbnails.test.js:8:34)
at Object.asyncJestTest (/Users/pharmosan/IdeaProjects/thumbnails-frontend/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:100:37)
at /Users/pharmosan/IdeaProjects/thumbnails-frontend/node_modules/jest-jasmine2/build/queueRunner.js:43:12
at new Promise (<anonymous>)
at mapper (/Users/pharmosan/IdeaProjects/thumbnails-frontend/node_modules/jest-jasmine2/build/queueRunner.js:26:19)
at /Users/pharmosan/IdeaProjects/thumbnails-frontend/node_modules/jest-jasmine2/build/queueRunner.js:73:41
at processTicksAndRejections (internal/process/task_queues.js:97:5)