Skip to content

Commit

Permalink
feat(test): add abortSignal test
Browse files Browse the repository at this point in the history
  • Loading branch information
edorgeville committed May 24, 2024
1 parent 825caf4 commit 0778005
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion test/appRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ export const appRouter = router({
.mutation(({ input }) => {
state.count += input;
return state.count;
})
}),
takesASecondToResolve: publicProcedure.query(async () => {
await new Promise(resolve => setTimeout(resolve, 2000));
return 'done';
})
});
13 changes: 13 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ test('countUp mutation', async () => {
expect(addTwo).toBe(3);
});

test('abortSignal is handled & event listeners cleaned up', async () => {
const controller = new AbortController();
const promise = client.takesASecondToResolve.query(undefined, {
signal: controller.signal
});

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

// Only the server should still be listening, client should have cleaned up
expect(mqttClient.listeners('message').length).toBe(1);
});

afterAll(async () => {
mqttClient.end();
broker.close();
Expand Down

0 comments on commit 0778005

Please sign in to comment.