-
I'm trying to use Update 1: It seems Prisma wasn't mocked and Test code: it('Should redirect to /curriculum when user is found', async () => {
const user = {
username: 'fakeUser',
password: 'fakePassword'
}
prismaMock.user.findFirst.mockResolvedValue(user)
userMiddleware.mockImplementation((req, _res, next) => {
req.user = null
next()
})
const signInCallback = signIn(req, res)
const value = await signInCallback({
account: {
provider: 'discord'
},
user: {
id: '123'
}
})
expect(value).toBe('/curriculum')
}) The function being tested: export const signIn =
(req: LoggedRequest & Request, res: NextApiResponse & Response) =>
async ({ account, user }: { account: Account; user: User }) => {
const { provider } = account
if (provider === 'discord') {
const c0d3User = await getUserSession(req, res)
const { access_token, expires_at, refresh_token } = account
// Connect to discord
if (c0d3User?.id) {
await updateRefreshandAccessTokens(
c0d3User.id,
user.id,
refresh_token!,
access_token!,
new Date(expires_at! * 1000)
)
// Cancel auth flow. Session won't be created
return '/discord/success'
}
const userInfo = await prisma.user.findFirst({
where: {
discordId: user.id
}
})
if (userInfo) {
req.session.userId = userInfo.id
// Cancel auth flow. Session won't be created
return '/curriculum'
}
return '/discord/404user'
}
return true
} |
Beta Was this translation helpful? Give feedback.
Answered by
flacial
Mar 19, 2022
Replies: 1 comment
-
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
flacial
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
prismaMock
has to be imported before importing the functions that useprisma
.