diff --git a/test/factory.ts b/test/factory.ts new file mode 100644 index 0000000..0402f73 --- /dev/null +++ b/test/factory.ts @@ -0,0 +1,48 @@ +import { createTRPCProxyClient } from '@trpc/client'; +import Aedes from 'aedes'; +import { once } from 'events'; +import mqtt from 'mqtt'; +import { createServer } from 'net'; + +import { createMQTTHandler } from '../src/adapter'; +import { mqttLink } from '../src/link'; +import { AppRouter, appRouter } from './appRouter'; + +export async function factory() { + const requestTopic = 'rpc/request'; + + const aedes = new Aedes(); + // aedes.on('publish', (packet, client) => console.log(packet.topic, packet.payload.toString())); + const broker = createServer(aedes.handle); + broker.listen(1883); + const mqttClient = mqtt.connect('mqtt://localhost'); + + createMQTTHandler({ + client: mqttClient, + requestTopic, + router: appRouter + }); + + const client = createTRPCProxyClient({ + links: [ + mqttLink({ + client: mqttClient, + requestTopic + }) + ] + }); + + await once(broker, 'listening'); + await once(mqttClient, 'connect'); + + return { + client, + broker, + mqttClient, + close() { + mqttClient.end(); + broker.close(); + aedes.close(); + } + }; +} diff --git a/test/index.test.ts b/test/index.test.ts index 1293c17..6cd4275 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,63 +1,45 @@ -import { createTRPCProxyClient } from '@trpc/client'; -import Aedes from 'aedes'; -import { once } from 'events'; -import mqtt from 'mqtt'; -import { createServer } from 'net'; - -import { createMQTTHandler } from '../src/adapter'; -import { mqttLink } from '../src/link'; -import { AppRouter, appRouter } from './appRouter'; - -const requestTopic = 'rpc/request'; - -const aedes = new Aedes(); -// aedes.on('publish', (packet, client) => console.log(packet.topic, packet.payload.toString())); -const broker = createServer(aedes.handle); -broker.listen(1883); -const mqttClient = mqtt.connect('mqtt://localhost'); - -createMQTTHandler({ - client: mqttClient, - requestTopic, - router: appRouter -}); - -const client = createTRPCProxyClient({ - links: [ - mqttLink({ - client: mqttClient, - requestTopic - }) - ] -}); +import { factory } from './factory'; -beforeAll(async () => { - await once(broker, 'listening'); - await once(mqttClient, 'connect'); -}); +test('broker is listening', async () => { + const { broker, close } = await factory(); -test('broker is listening', () => { expect(broker.listening).toBe(true); + + close(); }); -test('mqtt client is connected', () => { +test('mqtt client is connected', async () => { + const { mqttClient, close } = await factory(); + expect(mqttClient.connected).toBe(true); + + close(); }); test('greet query', async () => { + const { client, close } = await factory(); + const greeting = await client.greet.query('world'); expect(greeting).toEqual({ greeting: 'hello, world!' }); + + close(); }); test('countUp mutation', async () => { + const { client, close } = await factory(); + const addOne = await client.countUp.mutate(1); expect(addOne).toBe(1); const addTwo = await client.countUp.mutate(2); expect(addTwo).toBe(3); + + close(); }); test('abortSignal is handled', async () => { + const { client, close } = await factory(); + const controller = new AbortController(); const promise = client.slow.query(undefined, { signal: controller.signal @@ -65,10 +47,6 @@ test('abortSignal is handled', async () => { controller.abort(); await expect(promise).rejects.toThrow('aborted'); -}); -afterAll(async () => { - mqttClient.end(); - broker.close(); - aedes.close(); + close(); });