# jasmine-sse

> Test your Server-Sent-Event application with jasmine

Latest version **0.3.0** (published 2019-12-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install jasmine-sse
pnpm add jasmine-sse
yarn add jasmine-sse
bun add jasmine-sse
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2019-12-21 |
| First published | 2018-11-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 57.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Mickael Jeanroy |
| Maintainers | mjeanroy |
| Keywords | Jasmine, ServerSentEvent |

## Links

- npm: https://www.npmjs.com/package/jasmine-sse
- Repository: https://github.com/mjeanroy/jasmine-sse
- Homepage: https://github.com/mjeanroy/jasmine-sse#readme
- Issues: https://github.com/mjeanroy/jasmine-sse/issues
- npm.io page: https://npm.io/package/jasmine-sse

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 0.3.0 (latest) — 2019-12-21
- 0.2.0 — 2018-11-10
- 0.1.0 — 2018-11-02

## README

## Jasmine-SSE

[![Greenkeeper badge](https://badges.greenkeeper.io/mjeanroy/jasmine-sse.svg)](https://greenkeeper.io/)

A simple addon for [](Jasmine) 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:

```javascript
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`:

```javascript
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:

```javascript
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](https://developer.mozilla.org/en-US/docs/Web/API/EventSource)) 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.

---
_Source: https://npm.io/package/jasmine-sse · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
