@@ -7,7 +7,9 @@ import { NextRequest, NextResponse } from 'next/server';
77
88const publishableKey = 'pk_test_Y2xlcmsuaW5jbHVkZWQua2F0eWRpZC05Mi5sY2wuZGV2JA' ;
99const authenticateRequestMock = jest . fn ( ) . mockResolvedValue ( {
10- toAuth : ( ) => ( { } ) ,
10+ toAuth : ( ) => ( {
11+ debug : ( d : any ) => d ,
12+ } ) ,
1113 headers : new Headers ( ) ,
1214 publishableKey,
1315} ) ;
@@ -33,12 +35,15 @@ import { decryptClerkRequestData } from '../utils';
3335 * Disable console warnings about config matchers
3436 */
3537const consoleWarn = console . warn ;
36- global . console . warn = jest . fn ( ) ;
38+ const consoleLog = console . log ;
39+
3740beforeAll ( ( ) => {
3841 global . console . warn = jest . fn ( ) ;
42+ global . console . log = jest . fn ( ) ;
3943} ) ;
4044afterAll ( ( ) => {
4145 global . console . warn = consoleWarn ;
46+ global . console . log = consoleLog ;
4247} ) ;
4348
4449// Removing this mock will cause the clerkMiddleware tests to fail due to missing publishable key
@@ -168,24 +173,6 @@ describe('createRouteMatcher', () => {
168173 } ) ;
169174} ) ;
170175
171- describe ( 'authenticateRequest & handshake' , ( ) => {
172- beforeEach ( ( ) => {
173- authenticateRequestMock . mockClear ( ) ;
174- } ) ;
175-
176- it ( 'returns 307 and starts the handshake flow for handshake requestState status' , async ( ) => {
177- const mockLocationUrl = 'https://example.com' ;
178- authenticateRequestMock . mockResolvedValueOnce ( {
179- publishableKey,
180- status : AuthStatus . Handshake ,
181- headers : new Headers ( { Location : mockLocationUrl } ) ,
182- } ) ;
183- const resp = await clerkMiddleware ( ) ( mockRequest ( { url : '/protected' } ) , { } as NextFetchEvent ) ;
184- expect ( resp ?. status ) . toEqual ( 307 ) ;
185- expect ( resp ?. headers . get ( 'Location' ) ) . toEqual ( mockLocationUrl ) ;
186- } ) ;
187- } ) ;
188-
189176describe ( 'clerkMiddleware(params)' , ( ) => {
190177 it ( 'renders route as normally when used without params' , async ( ) => {
191178 const signInResp = await clerkMiddleware ( ) ( mockRequest ( { url : '/sign-in' } ) , { } as NextFetchEvent ) ;
@@ -203,7 +190,7 @@ describe('clerkMiddleware(params)', () => {
203190 expect ( signInResp ?. headers . get ( 'a-custom-header' ) ) . toEqual ( '1' ) ;
204191 } ) ;
205192
206- it ( 'renders route when when exported directly without being called' , async ( ) => {
193+ it ( 'renders route when exported directly without being called' , async ( ) => {
207194 // This is equivalent to export default clerkMiddleware;
208195 const signInResp = await clerkMiddleware ( mockRequest ( { url : '/sign-in' } ) , { } as NextFetchEvent ) ;
209196 expect ( signInResp ?. status ) . toEqual ( 200 ) ;
@@ -567,6 +554,32 @@ describe('clerkMiddleware(params)', () => {
567554 expect ( clerkClient ( ) . authenticateRequest ) . toBeCalled ( ) ;
568555 } ) ;
569556 } ) ;
557+
558+ describe ( 'debug' , ( ) => {
559+ beforeEach ( ( ) => {
560+ ( global . console . log as jest . Mock ) . mockClear ( ) ;
561+ } ) ;
562+
563+ it ( 'outputs debug logs when used with only params' , async ( ) => {
564+ const signInResp = await clerkMiddleware ( { debug : true } ) ( mockRequest ( { url : '/sign-in' } ) , { } as NextFetchEvent ) ;
565+ expect ( signInResp ?. status ) . toEqual ( 200 ) ;
566+ // 6 times results from header, footer, options, url, requestState, auth logs
567+ expect ( global . console . log ) . toBeCalledTimes ( 6 ) ;
568+ } ) ;
569+
570+ it ( 'outputs debug logs when used with a custom handler' , async ( ) => {
571+ const signInResp = await clerkMiddleware (
572+ ( _ , request ) => {
573+ expect ( request . url ) . toContain ( '/sign-in' ) ;
574+ return NextResponse . next ( { headers : { 'a-custom-header' : '1' } } ) ;
575+ } ,
576+ { debug : true } ,
577+ ) ( mockRequest ( { url : '/sign-in' } ) , { } as NextFetchEvent ) ;
578+ expect ( signInResp ?. status ) . toEqual ( 200 ) ;
579+ // 6 times results from header, footer, options, url, requestState, auth logs
580+ expect ( global . console . log ) . toBeCalledTimes ( 6 ) ;
581+ } ) ;
582+ } ) ;
570583} ) ;
571584
572585describe ( 'Dev Browser JWT when redirecting to cross origin for page requests' , function ( ) {
0 commit comments