Skip to content

Commit

Permalink
refactor(test): extract stack creation to external factory, recreate …
Browse files Browse the repository at this point in the history
…one for each test for better isolation
  • Loading branch information
edorgeville committed May 24, 2024
1 parent 8c12465 commit 8437a1c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 43 deletions.
48 changes: 48 additions & 0 deletions test/factory.ts
Original file line number Diff line number Diff line change
@@ -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<AppRouter>({
links: [
mqttLink({
client: mqttClient,
requestTopic
})
]
});

await once(broker, 'listening');
await once(mqttClient, 'connect');

return {
client,
broker,
mqttClient,
close() {
mqttClient.end();
broker.close();
aedes.close();
}
};
}
64 changes: 21 additions & 43 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,52 @@
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<AppRouter>({
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
});

controller.abort();
await expect(promise).rejects.toThrow('aborted');
});

afterAll(async () => {
mqttClient.end();
broker.close();
aedes.close();
close();
});

0 comments on commit 8437a1c

Please sign in to comment.