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 * refactor(test): wrap tests in `withFactory` wrapper, cleans up after itself in case of exceptions * feat: add `createContext` support & associated test
- Loading branch information
1 parent
8c12465
commit a02a567
Showing
4 changed files
with
124 additions
and
68 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
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
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,60 @@ | ||
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 { type AppRouter, appRouter, createContext } from './appRouter'; | ||
|
||
export 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, | ||
createContext | ||
}); | ||
|
||
const client = createTRPCProxyClient<AppRouter>({ | ||
links: [ | ||
mqttLink({ | ||
client: mqttClient, | ||
requestTopic | ||
}) | ||
] | ||
}); | ||
|
||
return { | ||
client, | ||
broker, | ||
mqttClient, | ||
async ready() { | ||
await once(broker, 'listening'); | ||
await once(mqttClient, 'connect'); | ||
}, | ||
close() { | ||
mqttClient.end(); | ||
broker.close(); | ||
aedes.close(); | ||
} | ||
}; | ||
} | ||
|
||
export async function withFactory(fn: (f: ReturnType<typeof factory>) => Promise<void>) { | ||
const f = factory(); | ||
await f.ready(); | ||
try { | ||
await fn(f); | ||
} finally { | ||
f.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,59 @@ | ||
import { createTRPCProxyClient } from '@trpc/client'; | ||
import Aedes from 'aedes'; | ||
import { once } from 'events'; | ||
import mqtt from 'mqtt'; | ||
import { createServer } from 'net'; | ||
import { withFactory } from './factory'; | ||
|
||
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 | ||
}) | ||
] | ||
}); | ||
|
||
beforeAll(async () => { | ||
await once(broker, 'listening'); | ||
await once(mqttClient, 'connect'); | ||
}); | ||
|
||
test('broker is listening', () => { | ||
expect(broker.listening).toBe(true); | ||
}); | ||
|
||
test('mqtt client is connected', () => { | ||
expect(mqttClient.connected).toBe(true); | ||
describe('broker', () => { | ||
test('is listening', async () => { | ||
await withFactory(async ({ broker }) => { | ||
expect(broker.listening).toBe(true); | ||
}); | ||
}); | ||
}); | ||
|
||
test('greet query', async () => { | ||
const greeting = await client.greet.query('world'); | ||
expect(greeting).toEqual({ greeting: 'hello, world!' }); | ||
describe('mqtt client', () => { | ||
test('is connected', async () => { | ||
await withFactory(async ({ mqttClient }) => { | ||
expect(mqttClient.connected).toBe(true); | ||
}); | ||
}); | ||
}); | ||
|
||
test('countUp mutation', async () => { | ||
const addOne = await client.countUp.mutate(1); | ||
expect(addOne).toBe(1); | ||
describe('procedures', () => { | ||
test('greet query', async () => { | ||
await withFactory(async ({ client }) => { | ||
const greeting = await client.greet.query('world'); | ||
expect(greeting).toEqual({ greeting: 'hello, world!' }); | ||
}); | ||
}); | ||
|
||
const addTwo = await client.countUp.mutate(2); | ||
expect(addTwo).toBe(3); | ||
}); | ||
test('countUp mutation', async () => { | ||
await withFactory(async ({ client }) => { | ||
const addOne = await client.countUp.mutate(1); | ||
expect(addOne).toBe(1); | ||
|
||
test('abortSignal is handled', async () => { | ||
const controller = new AbortController(); | ||
const promise = client.slow.query(undefined, { | ||
signal: controller.signal | ||
const addTwo = await client.countUp.mutate(2); | ||
expect(addTwo).toBe(3); | ||
}); | ||
}); | ||
|
||
controller.abort(); | ||
await expect(promise).rejects.toThrow('aborted'); | ||
describe('abort signal', () => { | ||
test('is handled', async () => { | ||
await withFactory(async ({ client }) => { | ||
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(); | ||
describe('context', () => { | ||
test('getContext query', async () => { | ||
await withFactory(async ({ client }) => { | ||
const ctx = await client.getContext.query(); | ||
expect(ctx).toEqual({ hello: 'world' }); | ||
}); | ||
}); | ||
}); |