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

jasmine-sse

v0.3.0

Published

Test your Server-Sent-Event application with jasmine

Downloads

4

Readme

Jasmine-SSE

Greenkeeper badge

A simple addon for to test a "Server Sent Event" application easily!

Installation

  • Install the latest version: npm run install --save-dev jasmine-sse.
  • Add the main entry point (i.e dist/jasmine-sse.js) file to your test suite.

Example

Suppose this (very) simple chat application:

let sse;
let messages = [];

function onNewMessage(e) {
  messages.push(e.data);
}

function connect() {
  if (sse) {
    return;
  }

  sse = new EventSource('/chat');
  sse.addEventListener('message', displayMessage);
}

function sendMessage(message) {
  fetch('/chat', {
    method: 'POST',
    body: JSON.stringify({message}),
    headers: {
      'Content-Type': 'application/json; charset=utf-8',
    },
  });
}

function disconnect() {
  if (sse) {
    sse.close();
    sse.removeEventListener('message', displayMessage);
    sse = null;
  }
}

Testing this application can be easy with jasmine-sse:

describe('app', () => {
  beforeEach(() => {
    jasmine.sse().install();
  });

  afterEach(() => {
    jasmine.sse().uninstall();
  });

  it('should connect sse', () => {
    connect();

    const connections = jasmine.sse().connections();
    expect(connections.count()).toBe(1);
    
    const connection = connections.mostRecent();
    expect(connection.url).toBe('...');
    expect(connection.readyState).toBe(0);
    expect(messages).toEqual([]);

    connection.emit('Hi dude');
    expect(messages).toEqual(['Hi dude']);

    // Or with an object containing event type.
    connection.emit({
      type: 'custom',
      message: 'Hi bob',
    });

    expect(messages).toEqual(['Hi dude']);

    disconnect();
    expect(sse.getEventListeners()).toEqual([]);
  });
});

API

jasmine.sse

jasmine.sse().install()

Install the fake EventSource implementation, typically called in a beforeEach method. Note that:

  • The fake implementation is installed if, and only if, a native EventSource implementaton is available (i.e if browser supports EventSource), otherwise this method do nothing (and do not fail).
  • This method will fail if jasmine-sse has already been installed.

jasmine.sse().uninstall()

Install the fake EventSource implementation, typically called in a beforeEach method. Note that:

  • Like the jasmine.sse().install() method, if browser does not support native EventSource this method do nothing (and do not fail).
  • This method will fail if jasmine-sse has not been previously installed.

jasmine.sse().connections()

Returns an object containing methods to get tracked connection:

  • count(): number Get the number of tracked connections.
  • all(): Array<FakeEventSource> Get an array of all tracked connections.
  • first(): FakeEventSource Get the first tracked connections or undefined.
  • last(): FakeEventSource Get the last tracked connections or undefined.
  • at(idx: number): FakeEventSource Get the tracked connection at given index or undefined.

jasmine.sse().withMock(testFn)

Install the fake EventSource implementation, execute the test function testFn, then reset the fake implementation. This method can be used to install/uninstall fake EventSource API in a single test, for example:

it('should run test with fake implementation', () => {
  jasmine.sse().withMock(() => {
    doYourTest();
    doYourExpect();
  });
});

FakeEventSource

A tracked connection is a fake EventSource (so contains all methods of EventSource object as documented here) with additional methods:

  • emit(message: string|object): void Emit a message from the server.
  • failConnection(): void Fail the connection, typically used to test a connection failure from server.
  • reestablishConnection(): Array<string> Reestablish connection, typically used to test connection establishment.
  • getEventListeners(eventType?: string): Array<function> Get registered listeners (passing string parameter will return registered event listeners for given event type).

Licence

MIT License (MIT)

Contributing

If you find a bug or you think something is missing, feel free to contribute and submit an issue or a pull request.