Skip to content

Commit 0778005

Browse files
committed
feat(test): add abortSignal test
1 parent 825caf4 commit 0778005

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

test/appRouter.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,9 @@ export const appRouter = router({
2424
.mutation(({ input }) => {
2525
state.count += input;
2626
return state.count;
27-
})
27+
}),
28+
takesASecondToResolve: publicProcedure.query(async () => {
29+
await new Promise(resolve => setTimeout(resolve, 2000));
30+
return 'done';
31+
})
2832
});

test/index.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ test('countUp mutation', async () => {
5757
expect(addTwo).toBe(3);
5858
});
5959

60+
test('abortSignal is handled & event listeners cleaned up', async () => {
61+
const controller = new AbortController();
62+
const promise = client.takesASecondToResolve.query(undefined, {
63+
signal: controller.signal
64+
});
65+
66+
controller.abort();
67+
await expect(promise).rejects.toThrow('aborted');
68+
69+
// Only the server should still be listening, client should have cleaned up
70+
expect(mqttClient.listeners('message').length).toBe(1);
71+
});
72+
6073
afterAll(async () => {
6174
mqttClient.end();
6275
broker.close();

0 commit comments

Comments
 (0)