alimcg1
alimcg1
TTCTheo's Typesafe Cult
Created by alimcg1 on 1/27/2024 in #questions
Jest test for tRPC route
Hi guys, I have been using Jest to test tRPC routes in a project like this:
import { expect, it } from "@jest/globals";
import { inferProcedureInput } from "@trpc/server";
import { prisma } from "../../../../server/db";
import { AppRouter, appRouter } from "../../root";

it("tests the example hello router", async () => {
const caller = appRouter.createCaller({ session: null, prisma: prisma });

type Input = inferProcedureInput<AppRouter["example"]["hello"]>;

const input: Input = {
text: "test",
};

const result = await caller.example.hello(input);

expect(result).toStrictEqual({ greeting: "Hello test"})
});
import { expect, it } from "@jest/globals";
import { inferProcedureInput } from "@trpc/server";
import { prisma } from "../../../../server/db";
import { AppRouter, appRouter } from "../../root";

it("tests the example hello router", async () => {
const caller = appRouter.createCaller({ session: null, prisma: prisma });

type Input = inferProcedureInput<AppRouter["example"]["hello"]>;

const input: Input = {
text: "test",
};

const result = await caller.example.hello(input);

expect(result).toStrictEqual({ greeting: "Hello test"})
});
With "@trpc/server": "^10.9.0" - everything works as expected. However in a new project using the t-3 App router from npm create t3-app@latest which has "@trpc/server": "^10.43.6",: 'createCaller' is deprecated.ts(6385) The types file for router.d.ts say to use t.createCallerFactory(router) instead and links to the docs: https://trpc.io/docs/server/server-side-calls But I am struggling to make this work within the t-3 set up. Has anyone else had this problem? Or any suggestions on how to fix it/ work around it? Thanks
1 replies