import { describe, expect, test } from '@jest/globals';
-import { IrcServer } from './util/irc-server';
+import { TestIrcServer } from '..';
-IrcServer.describe('Client', (server) => {
+TestIrcServer.describe('Client', (server) => {
describe('joining channels', () => {
test('will get a join event from a newly joined user', async () => {
const [speaker, listener] = server().clients;
import { test, expect } from '@jest/globals';
-import { IrcServer, TestClient } from './util/irc-server';
+import { TestIrcServer, TestClient } from '..';
import { DefaultIrcSupported, IrcConnection, IrcInMemoryState } from '../src';
import { createConnection } from 'net';
}
}
-IrcServer.describe('Client with external connection', (getServer) => {
+TestIrcServer.describe('Client with external connection', (getServer) => {
let client: TestClient;
test('can connect with a fresh session', async () => {
const server = getServer();
const inMemoryState = new TestIrcInMemoryState(DefaultIrcSupported);
- client = new TestClient(server.address, IrcServer.generateUniqueNick("mynick"), {
+ client = new TestClient(server.address, TestIrcServer.generateUniqueNick("mynick"), {
port: server.port,
autoConnect: false,
connectionTimeout: 4000,
port: server.port,
host: server.address,
}) as IrcConnection;
- client = new TestClient(server.address, IrcServer.generateUniqueNick("mynick"), {
+ client = new TestClient(server.address, TestIrcServer.generateUniqueNick("mynick"), {
port: server.port,
autoConnect: false,
connectionTimeout: 4000,
client.destroy();
// Somehow we need to clear away the client.
- const reusedClient = new TestClient(server.address, IrcServer.generateUniqueNick("mynick"), {
+ const reusedClient = new TestClient(server.address, TestIrcServer.generateUniqueNick("mynick"), {
port: server.port,
autoConnect: false,
connectionTimeout: 4000,
const DEFAULT_PORT = parseInt(process.env.IRC_TEST_PORT ?? '6667', 10);
const DEFAULT_ADDRESS = process.env.IRC_TEST_ADDRESS ?? "127.0.0.1";
+/**
+ * Exposes a client instance with helper methods to listen
+ * for events.
+ */
export class TestClient extends Client {
public readonly errors: Message[] = [];
}
}
-export class IrcServer {
+/**
+ * A jest-compatible test rig that can be used to run tests against an IRC server.
+ *
+ * @example
+ * ```ts
+ * IrcServer.describe('Client', (server) => {
+ describe('joining channels', () => {
+ test('will get a join event from a newly joined user', async () => {
+ const [speaker, listener] = server().clients;
+
+ // Join the room and listen
+ const listenerJoinPromise = listener.waitForEvent('join');
+ await listener.join('#foobar');
+ const [lChannel, lNick] = await listenerJoinPromise;
+ expect(lNick).toBe(listener.nick);
+ expect(lChannel).toBe('#foobar');
+
+ const speakerJoinPromise = listener.waitForEvent('join');
+ await speaker.join('#foobar');
+ const [channel, nick] = await speakerJoinPromise;
+ expect(nick).toBe(speaker.nick);
+ expect(channel).toBe('#foobar');
+ });
+ });
+ });
+ * ```
+ */
+export class TestIrcServer {
/**
* Test wrapper that automatically provisions an IRC server
* @param fn The inner function
* @returns A jest describe function.
*/
- static describe(name: string, fn: (server: () => IrcServer) => void, opts?: {clients: string[]}) {
+ static describe(name: string, fn: (server: () => TestIrcServer) => void, opts?: {clients: string[]}) {
return describe(name, () => {
- let server: IrcServer;
+ let server: TestIrcServer;
beforeEach(async () => {
- server = new IrcServer();
+ server = new TestIrcServer();
await server.setUp(opts?.clients);
});
afterEach(async () => {
const connections: Promise<void>[] = [];
for (let index = 0; index < clients.length; index++) {
const client =
- new TestClient(this.address, IrcServer.generateUniqueNick(clients[index]), {
+ new TestClient(this.address, TestIrcServer.generateUniqueNick(clients[index]), {
port: this.port,
autoConnect: false,
connectionTimeout: 4000,