Mugetsu
Mugetsu2y ago

Vitest error test

How can I test for specific TRPCError code with vitest? I managed to get the message but can't figure out how to get the code.
it('should throw if user roles are missing', async ({ trpcContextMock }) => {
const ctx = await createInnerTRPCContext(
trpcContextMock({ authorisation: { userSecurityGroups: [] } }),
)
const caller = appRouter.createCaller(ctx)

await expect(caller.locations.searchLocations({})).rejects.toThrowError(
'User roles are missing',
)
})
it('should throw if user roles are missing', async ({ trpcContextMock }) => {
const ctx = await createInnerTRPCContext(
trpcContextMock({ authorisation: { userSecurityGroups: [] } }),
)
const caller = appRouter.createCaller(ctx)

await expect(caller.locations.searchLocations({})).rejects.toThrowError(
'User roles are missing',
)
})
2 Replies
Nick
Nick2y ago
tRPC actually uses vitest in its own codebase, you could probably find a lot of helpful boilerplate in there 🙂
Mugetsu
MugetsuOP2y ago
Ahh well. Silly me I didn't check this earlier. Thanks for suggestion ! I found solution in a second ❤️

Did you find this page helpful?