forked from imxeno/trpc-rabbitmq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(test): extract stack creation to external factory, recreate …
…one for each test for better isolation
- Loading branch information
1 parent
8c12465
commit 8437a1c
Showing
2 changed files
with
69 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |