Anybody got a vitest with T3-app setup?

I'm trying to setup vitest tests with my trpc routes, but after following the official t3 docs (https://create.t3.gg/en/usage/trpc/#sample-integration-test), and the official prisma guide on how to mock the db (https://www.prisma.io/blog/testing-series-1-8eRB5p0Y8o), I am running into the issue of (it seems like) my models not being created. TypeError: Cannot read properties of undefined (reading 'create') Does anybody have any projects that have this setup and working? Been picking my brain on this for a few hours and can't understand why my prisma mock isn't working as expected.
1 Reply
Xaohs
Xaohs15mo ago
This is my mock directly taken from the prisma docs
import { type PrismaClient } from '@prisma/client';
import { beforeEach } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';

beforeEach(() => {
mockReset(prisma);
});

const prisma = mockDeep<PrismaClient>();
export default prisma;
import { type PrismaClient } from '@prisma/client';
import { beforeEach } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';

beforeEach(() => {
mockReset(prisma);
});

const prisma = mockDeep<PrismaClient>();
export default prisma;
And this is the (test) test i'm trying to execute.
beforeEach(() => {
vi.restoreAllMocks();
});

const newUser = {
id: '1',
emailVerified: new Date(),
image: null,
name: 'Prisma Fan',
};
prisma.user.create.mockResolvedValue({
...newUser,
});
const createUser = async (user: typeof newUser) => {
return await prisma.user.create({
data: user,
});
};

const user = await createUser(newUser);
expect(user).toStrictEqual({ ...newUser, id: 1 });
beforeEach(() => {
vi.restoreAllMocks();
});

const newUser = {
id: '1',
emailVerified: new Date(),
image: null,
name: 'Prisma Fan',
};
prisma.user.create.mockResolvedValue({
...newUser,
});
const createUser = async (user: typeof newUser) => {
return await prisma.user.create({
data: user,
});
};

const user = await createUser(newUser);
expect(user).toStrictEqual({ ...newUser, id: 1 });
This is very barebones, as this doesn't use TRPC yet, when adding this prisma mock to the innerContext, and calling a trpc route, I get the same create error. But I want this example to be simple for now.
Want results from more Discord servers?
Add your server