You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before noticing that I could use withAuth, I was using getToken which I successfully mocked like this:
// middleware.spec.tsimport*asnextAuthJwtModulefrom"next-auth/jwt";constmockGetToken=jest.fn<Promise<CustomJWT>,any>(async()=>newPromise((resolve)=>{resolve({// ... my custom token content});}));jest.spyOn(nextAuthJwtModule,"getToken").mockImplementation(mockGetToken);awaitmiddleware(mockedRequest);expect(mockGetToken).toBeCalled()
But when I try do do the mock withAuth:
import*asnextAuthModulefrom'next-auth/next/middleware';// Also tried with import * as nextAuthModule from 'next-auth/middleware';mockWithAuth=jest.fn((f,p)=>{throwError('mock with auth');});jest.spyOn(nextAuthModule,'withAuth').mockImplementation(mockWithAuth);awaitmiddleware(mockedRequest);expect(mockWithAuth).toBeCalled();
When I run the test above, it fails during middleware execution saying:
This error is relevant since I did not configure next auth in my test environment. This only shows that the mock is not working and the real withAuth function is executed instead.
Has anyone been successful mocking withAuth using jest?
I was able to get it working by creating a file under /__mocks__ :
// __mocks__/next-auth/middleware/index.tsimport{NextAuthMiddlewareOptions}from'next-auth/middleware';import{NextRequest,NextResponse}from'next/server';import{NextRequestWithAuthData}from'types/auth';typePartialMiddleware=(req: NextRequestWithAuthData,)=>Promise<NextResponse>;exportconstwithAuth=(middleware: PartialMiddleware,opts: NextAuthMiddlewareOptions,)=>{return(req: NextRequest)=>{if(opts){constallowed=opts.callbacks.authorized({
req,token: {// This should be mockable},});if(!allowed)returnfalse;}returnmiddleware({
...req,nextauth: {token: {// This should be mockable too},},});};};
What I don't like with this approach is that I cannot change the values based on my scenario (e.g. if user is authenticated or not). I have control on the request data since I pass a mock request to the middleware.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there !
I'm currently facing troubles testing a middleware in my NextJS app. Here's a simplified implementation:
Before noticing that I could use
withAuth
, I was usinggetToken
which I successfully mocked like this:But when I try do do the mock withAuth:
When I run the test above, it fails during middleware execution saying:
This error is relevant since I did not configure next auth in my test environment. This only shows that the mock is not working and the real
withAuth
function is executed instead.Has anyone been successful mocking
withAuth
using jest?I was able to get it working by creating a file under
/__mocks__
:What I don't like with this approach is that I cannot change the values based on my scenario (e.g. if user is authenticated or not). I have control on the request data since I pass a mock request to the middleware.
Any clues anyone?
Beta Was this translation helpful? Give feedback.
All reactions