@@ -4,28 +4,14 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
44
55import { authFnContext } from '../clerkMiddleware' ;
66import { getAuth } from '../getAuth' ;
7- import { legacyAuthenticateRequest } from '../legacyAuthenticateRequest' ;
8-
9- vi . mock ( '../legacyAuthenticateRequest' , ( ) => {
10- return {
11- legacyAuthenticateRequest : vi . fn ( ) . mockResolvedValue ( {
12- toAuth : vi . fn ( ) . mockImplementation ( ( ) => ( {
13- userId : 'user_xxx' ,
14- tokenType : TokenType . SessionToken ,
15- } ) ) ,
16- headers : new Headers ( ) ,
17- status : 'signed-in' ,
18- } ) ,
19- } ;
20- } ) ;
217
228describe ( 'getAuth' , ( ) => {
239 beforeEach ( ( ) => {
2410 vi . clearAllMocks ( ) ;
2511 process . env . CLERK_SECRET_KEY = 'sk_test_...' ;
2612 } ) ;
2713
28- it ( 'should not call legacyAuthenticateRequest when middleware context exists' , async ( ) => {
14+ it ( 'should work when middleware context exists' , async ( ) => {
2915 const mockContext = {
3016 get : vi . fn ( ) . mockImplementation ( contextKey => {
3117 if ( contextKey === authFnContext ) {
@@ -47,12 +33,11 @@ describe('getAuth', () => {
4733
4834 const auth = await getAuth ( args ) ;
4935
50- expect ( legacyAuthenticateRequest ) . not . toHaveBeenCalled ( ) ;
5136 expect ( auth . userId ) . toBe ( 'user_xxx' ) ;
5237 expect ( auth . tokenType ) . toBe ( 'session_token' ) ;
5338 } ) ;
5439
55- it ( 'should call legacyAuthenticateRequest when middleware context is missing' , async ( ) => {
40+ it ( 'should throw an error when middleware context is missing' , async ( ) => {
5641 const mockContext = {
5742 get : vi . fn ( ) . mockReturnValue ( null ) ,
5843 } ;
@@ -62,10 +47,6 @@ describe('getAuth', () => {
6247 request : new Request ( 'http://clerk.com' ) ,
6348 } as LoaderFunctionArgs ;
6449
65- const auth = await getAuth ( args ) ;
66-
67- expect ( legacyAuthenticateRequest ) . toHaveBeenCalled ( ) ;
68- expect ( auth . userId ) . toBe ( 'user_xxx' ) ;
69- expect ( auth . tokenType ) . toBe ( 'session_token' ) ;
50+ await expect ( getAuth ( args ) ) . rejects . toThrow ( 'Clerk: clerkMiddleware() not detected' ) ;
7051 } ) ;
7152} ) ;
0 commit comments