jasmine-sse
v0.3.0
Published
Test your Server-Sent-Event application with jasmine
Downloads
4
Maintainers
Readme
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 = [];
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 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:
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.