Skip to content

Commit 544ddf1

Browse files
committed
feat(test): implement automatic testing
1 parent 0f11060 commit 544ddf1

File tree

5 files changed

+85
-45
lines changed

5 files changed

+85
-45
lines changed

test/appRouter.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,22 @@ const t = initTRPC.create();
77
const publicProcedure = t.procedure;
88
const router = t.router;
99

10+
const state = { count: 0 };
11+
1012
export const appRouter = router({
1113
greet: publicProcedure
1214
.input((val: unknown) => {
1315
if (typeof val === 'string') return val;
1416
throw new Error(`Invalid input: ${typeof val}`);
1517
})
16-
.query(({ input }) => ({ greeting: `hello, ${input}!` }))
18+
.query(({ input }) => ({ greeting: `hello, ${input}!` })),
19+
countUp: publicProcedure
20+
.input((val: unknown) => {
21+
if (typeof val === 'number') return val;
22+
throw new Error(`Invalid input: ${typeof val}`);
23+
})
24+
.mutation(({ input }) => {
25+
state.count += input;
26+
return state.count;
27+
})
1728
});

test/broker.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Aedes from 'aedes';
2+
import { createServer } from 'net';
3+
4+
const aedes = new Aedes();
5+
const server = createServer(aedes.handle);
6+
7+
server.listen(1883, () => {
8+
console.log('server started and listening on port 1883');
9+
});

test/client.ts

-26
This file was deleted.

test/index.test.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { createTRPCProxyClient } from '@trpc/client';
2+
import Aedes from 'aedes';
3+
import { once } from 'events';
4+
import mqtt from 'mqtt';
5+
import { createServer } from 'net';
6+
7+
import { createMQTTHandler } from '../src/adapter';
8+
import { mqttLink } from '../src/link';
9+
import { AppRouter, appRouter } from './appRouter';
10+
11+
const requestTopic = 'rpc/request';
12+
13+
const aedes = new Aedes();
14+
// aedes.on('publish', (packet, client) => console.log(packet.topic, packet.payload.toString()));
15+
const broker = createServer(aedes.handle);
16+
broker.listen(1883);
17+
const mqttClient = mqtt.connect('mqtt://localhost');
18+
19+
createMQTTHandler({
20+
client: mqttClient,
21+
requestTopic,
22+
router: appRouter
23+
});
24+
25+
const client = createTRPCProxyClient<AppRouter>({
26+
links: [
27+
mqttLink({
28+
client: mqttClient,
29+
requestTopic
30+
})
31+
]
32+
});
33+
34+
beforeAll(async () => {
35+
await once(broker, 'listening');
36+
await once(mqttClient, 'connect');
37+
});
38+
39+
test('broker is listening', () => {
40+
expect(broker.listening).toBe(true);
41+
});
42+
43+
test('mqtt client is connected', () => {
44+
expect(mqttClient.connected).toBe(true);
45+
});
46+
47+
test('greet query', async () => {
48+
const greeting = await client.greet.query('world');
49+
expect(greeting).toEqual({ greeting: 'hello, world!' });
50+
});
51+
52+
test('countUp mutation', async () => {
53+
const addOne = await client.countUp.mutate(1);
54+
expect(addOne).toBe(1);
55+
56+
const addTwo = await client.countUp.mutate(2);
57+
expect(addTwo).toBe(3);
58+
});
59+
60+
afterAll(async () => {
61+
mqttClient.end();
62+
broker.close();
63+
aedes.close();
64+
});

test/server.ts

-18
This file was deleted.

0 commit comments

Comments
 (0)