Skip to content

How to mock useSession with v4? #4185

Answered by agangdi
agangdi asked this question in Help
Mar 15, 2022 · 5 comments · 9 replies
Discussion options

You must be logged in to vote

finally, I fixed it myself, it's because v4 change useSession return type to object {data: null, status: "authenticated}. so I changed by test code like this

import { render, screen } from '@testing-library/react'
import Header from '../../components/layout/Header'
import "@testing-library/jest-dom";
import {useSession} from "next-auth/react";

jest.mock("next-auth/react", () => {
  const originalModule = jest.requireActual('next-auth/react');
  const mockSession = {
    expires: new Date(Date.now() + 2 * 86400).toISOString(),
    user: { username: "admin" }
  };
  return {
    __esModule: true,
    ...originalModule,
    useSession: jest.fn(() => {
      return {data: mockSession, status: 

Replies: 5 comments 9 replies

Comment options

You must be logged in to vote
7 replies
@DoctorDerek
Comment options

@agangdi
Comment options

@pnts-se
Comment options

@KaidiMohammed
Comment options

@Aroooj
Comment options

Answer selected by agangdi
Comment options

You must be logged in to vote
2 replies
@flux0uz
Comment options

@timtyrrell
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
question Ask how to do something or how something works
Converted from issue

This discussion was converted from issue #4184 on March 15, 2022 04:09.