Typing a shared TRPC provider for testing
Context:
- We have a monorepo with several micro-frontends which use Next.
- We're moving to using tRPC to communicate between the front end and Next backend
- We test our components with React Testing Library
- We have a shared package for our testing utils, including a render method that renders a component for testing with all required providers (Theme, i18n, etc etc)
Problem:
We want to add a tRPC provider to the providers that our custom render method wraps around the component under test, so that components that use tRPC queries/mutations can be tested with RTL. The basic idea is something like this:
but TypeScript complains about
testTRPC.createClient
:
and testTRPC.Provider
:
I'm guessing the usage of AnyRouter
is the problem here, but really not sure what type I can provide here, since it could be the AppRouter
from any one of our micro-frontends - perhaps something using generics?7 Replies
I have this exact problem!
Ended up putting the code in
src
but in the package root I have index.ts
that exports everything I want to make available. Super simple tsconfig. I'm sure there's another way but I gave up.@itsravenous any chance you've found a fix for this? I also have the same issue 😦
Nope, sorry - I just ended up adding an
@ts-expect-error
comment and forgetting about it!All g, thanks for the reply. Any chance this was an open source project you were working on that you could share the repo to?
Sorry, it's a client project
All g, thanks anyways!
👍 Essentially the code snippet above should be enough, and the add
@ts-expect-error
comments to squish tRPC/Typescript's complaints.