Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
jasmine-sse
Advanced tools
A simple addon for to test a "Server Sent Event" application easily!
npm run install --save-dev jasmine-sse
.main
entry point (i.e dist/jasmine-sse.js
) file to your test suite.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([]);
});
});
jasmine.sse
jasmine.sse().install()
Install the fake EventSource
implementation, typically called in a beforeEach
method.
Note that:
EventSource
implementaton is available (i.e if browser supports EventSource
), otherwise this method do nothing (and do not fail).jasmine-sse
has already been installed.jasmine.sse().uninstall()
Install the fake EventSource
implementation, typically called in a beforeEach
method.
Note that:
jasmine.sse().install()
method, if browser does not support native EventSource
this method do nothing (and do not fail).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).MIT License (MIT)
If you find a bug or you think something is missing, feel free to contribute and submit an issue or a pull request.
FAQs
Test your Server-Sent-Event application with jasmine
We found that jasmine-sse demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.