tRPCttRPC
Powered by
itsravenousI
tRPC•3y ago•
9 replies
itsravenous

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:

import { AnyRouter } from '@trpc/server';

export const testTRPC = createTRPCReact<AnyRouter>();

export const TRPCWrapper = ({ children }: { children: ReactNode }) => {
  const queryClient = new QueryClient();

  const trpcClient = testTRPC.createClient({
    links: [
      httpLink({
        url: `http://localhost/trpc`,
      }),
    ],
  });

  return (
    <testTRPC.Provider client={trpcClient} queryClient={queryClient}>
      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
    </testTRPC.Provider>
  );
};
import { AnyRouter } from '@trpc/server';

export const testTRPC = createTRPCReact<AnyRouter>();

export const TRPCWrapper = ({ children }: { children: ReactNode }) => {
  const queryClient = new QueryClient();

  const trpcClient = testTRPC.createClient({
    links: [
      httpLink({
        url: `http://localhost/trpc`,
      }),
    ],
  });

  return (
    <testTRPC.Provider client={trpcClient} queryClient={queryClient}>
      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
    </testTRPC.Provider>
  );
};


but TypeScript complains about
testTRPC.createClient
testTRPC.createClient
:

Property 'createClient' does not exist on type '"The property 'useContext' in your router collides with a built-in method, rename this router or procedure on your backend." | "The property 'Provider' in your router collides with a built-in method, rename this router or procedure on your backend." | "The property 'createClient' in your router collides with a built...'.
Property 'createClient' does not exist on type '"The property 'useContext' in your router collides with a built-in method, rename this router or procedure on your backend." | "The property 'Provider' in your router collides with a built-in method, rename this router or procedure on your backend." | "The property 'createClient' in your router collides with a built...'.


and
testTRPC.Provider
testTRPC.Provider
:

Property 'Provider' does not exist on type '"The property 'useContext' in your router collides with a built-in method, rename this router or procedure on your backend." | "The property 'Provider' in your router collides with a built-in method, rename this router or procedure on your backend." | "The property 'createClient' in your router collides with a built...'.
Property 'Provider' does not exist on type '"The property 'useContext' in your router collides with a built-in method, rename this router or procedure on your backend." | "The property 'Provider' in your router collides with a built-in method, rename this router or procedure on your backend." | "The property 'createClient' in your router collides with a built...'.


I'm guessing the usage of
AnyRouter
AnyRouter
is the problem here, but really not sure what type I can provide here, since it could be the
AppRouter
AppRouter
from any one of our micro-frontends - perhaps something using generics?
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

testing a trpc hook
SonSSon / ❓-help
4y ago
Typing on trpc.createClient?
hashwarpHhashwarp / ❓-help
17mo ago
How does trpc typing work
Mohammed AnasMMohammed Anas / ❓-help
3y ago
Monorepo with shared trpc router type
MugetsuMMugetsu / ❓-help
8mo ago