P
Prisma4mo ago
Mirfayz

How to test a prisma call that uses `connectOrCreate`? (using vitest ideally)

Hey all! I'm trying to write a unit test for the following prisma call in my Next.js app:
export async function createInterview(data: {
participantIdentifier?: string;
protocolId: string;
}) {
const { participantIdentifier, protocolId } = data;

const createdInterview = await prisma.interview.create({
select: {
participant: true,
id: true,
},
data: {
network: Prisma.JsonNull,
participant: {
connectOrCreate: {
create: {
identifier: participantIdentifier!,
},
where: {
identifier: participantIdentifier,
},
},
},
protocol: {
connect: {
id: protocolId,
},
},
},
});

return {
error: null,
createdInterviewId: createdInterview.id,
errorType: null,
};
}
export async function createInterview(data: {
participantIdentifier?: string;
protocolId: string;
}) {
const { participantIdentifier, protocolId } = data;

const createdInterview = await prisma.interview.create({
select: {
participant: true,
id: true,
},
data: {
network: Prisma.JsonNull,
participant: {
connectOrCreate: {
create: {
identifier: participantIdentifier!,
},
where: {
identifier: participantIdentifier,
},
},
},
protocol: {
connect: {
id: protocolId,
},
},
},
});

return {
error: null,
createdInterviewId: createdInterview.id,
errorType: null,
};
}
Basically, I want to test this case: "It should connect a participant if an existing identifier is provided or create a participant if an identifier doesn't exist in the db'" Is there any way of testing this? Maybe, mocking a DB state and using it to provide state or smt? Would appreciate your help, thank you in advance!
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server