Jasmine-SSE
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.edist/jasmine-sse.js
) file to your test suite.
Example
Suppose this (very) simple chat application:
let sse;let messages = ; { messages;} { if sse return; sse = '/chat'; sse;} { ;} { if sse sse; sse; sse = null; }
Testing this application can be easy with jasmine-sse
:
;
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 supportsEventSource
), 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 nativeEventSource
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 orundefined
.last(): FakeEventSource
Get the last tracked connections orundefined
.at(idx: number): FakeEventSource
Get the tracked connection at given index orundefined
.
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:
;
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.