npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dimak.dev/event-source-mock

v1.0.0

Published

EventSource Mock

Downloads

9

Readme

EventSource Mock

This repository provides a mock implementation of the EventSource interface in JavaScript, designed to facilitate unit testing for applications that rely on server-sent events (SSE). One common issue faced by developers is the lack of a native EventSource implementation in Node.js environments, where EventSource is not defined. This mock addresses that gap, allowing developers to simulate and test the behavior of EventSource without requiring an actual server connection.

Features

  • Simulate Server-Sent Events: Easily trigger events such as message, open, and error.
  • Customizable Event Data: Define the data payload for each event to test various scenarios.
  • Integration with Testing Frameworks: Compatible with popular JavaScript testing frameworks like Jest, Mocha, and Jasmine.

Installation

To install the mock, you can use NPM:

npm install @dimak.dev/event-source-mock

or Yarn:

yarn add @dimak.dev/event-source-mock

or PNPM:

pnpm add @dimak.dev/event-source-mock

Usage

Below is a basic example of how to use the EventSourceMock in your unit tests.

Setting Up the Mock

To enable EventSourceMock in your test:

import eventSourceMock from '@dimak.dev/event-source-mock';
// OR
const eventSourceMock = require('@dimak.dev/event-source-mock').default;

describe('Your test', () => {
  beforeAll(() => {
    eventSourceMock.mockInit();
  });
});

And now you can test your code with EventSource.

Teardown the test environment

After test execution it is recommended to restoring default implementation of EventSource. To do this:

import eventSourceMock from '@dimak.dev/event-source-mock';
// OR
const eventSourceMock = require('@dimak.dev/event-source-mock').default;

describe('Your test', () => {
  afterAll(() => {
    eventSourceMock.mockRestore();
  });
});

Dispatch event

Example Test Case

Here's an example of how you might use the mock in a Jest test case:

describe('Server-Side events', () => {
  beforeAll(() => {
    eventSourceMock.mockInit();
  });

  afterAll(() => {
    eventSourceMock.mockRestore();
  });

  test('should handle server-sent events correctly', () => {
    const url = 'https://example.com/events';
    const eventSource = new EventSource(url);

    const onOpenHandler = jest.fn();
    const onMessageHandler = jest.fn();

    // Subscribe on events
    eventSource.addEventListener('open', onOpenHandler);
    eventSource.addEventListener('message', onMessageHandler);

    // Simulate an open connection
    eventSource.dispatchEvent(new Event('open'));

    // Simulate receiving a message
    const eventData = {data: JSON.stringify({foo: 'bar'})};
    const messageEvent = new MessageEvent('message', eventData);
    eventSource.dispatchEvent(messageEvent);

    // Assertions
    expect(onOpenHandler).toHaveBeenCalledTimes(1);
    expect(onMessageHandler).toHaveBeenCalledTimes(1);
    expect(onMessageHandler).toHaveBeenCalledWith(expect.objectContaining({
      data: '{"foo":"bar"}'
    }));
  });
});

For other examples you are welcomed to ./examples directory.

API Reference

EventSourceMock

The main class that mimics the behavior of the native EventSource and will be accessible after initialization over EventSource.

  • Constructor: new EventSource(url: string, options?: object)
    • url: The URL to which the connection would be made.
    • options: Optional configuration object.

eventSourceMock

The instantiated wrapper for mock. It is possible to operate the behaviour the instances of EventSourceMock.

Methods

  • mockInit(): void

    • Initializes the mock.
  • mockReset(): void

    • Resets all internal things for operating with mock.
  • mockRestore(): void

    • Restores native implementation of EventSource.

Properties

  • instances: Array<EventSourceMockInstance>

    • All instantiated EventSourceMocks.
  • latestInstance: EventSourceMockInstance | undefined

    • The latest instantiated EventSourceMock.

EventSourceMockInstance

Wrapped instance of EventSourceMock and accessible over eventSourceMock.instances or eventSourceMock.latestInstance.

Methods

  • open(dispatchEvent?: boolean, customOpenEvent?: Event): void

    • Imitates opening the connection.
    • dispatchEvent: Dispatch onopen event or not. Default: false
    • customOpenEvent: Custom event, If not present - event will be created
  • dispatchEvent(event: Event | MessageEvent)

    • Triggers an event of the specified type (open, message, or error) with optional initialization data.

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss any changes or improvements you would like to see.

License

This project is licensed under the MIT License. See the LICENSE file for details.


This README provides a comprehensive overview of how to use and integrate your EventSource mock into unit tests. Adjust any sections as needed based on specific features or configurations unique to your implementation.