@@ -4,17 +4,22 @@ import { beforeEach, describe, expect, test, vi } from 'vitest';
44
55import { clerkPlugin , getAuth } from '../index' ;
66
7- const authenticateRequestMock = vi . fn ( ) ;
7+ const { authenticateRequestMock, createClerkClientMock } = vi . hoisted ( ( ) => {
8+ const authenticateRequestMock = vi . fn ( ) ;
9+ const createClerkClientMock = vi . fn ( ( ) => {
10+ return {
11+ authenticateRequest : ( ...args : any ) => authenticateRequestMock ( ...args ) ,
12+ } ;
13+ } ) ;
14+
15+ return { authenticateRequestMock, createClerkClientMock } ;
16+ } ) ;
817
918vi . mock ( '@clerk/backend' , async ( ) => {
1019 const actual = await vi . importActual ( '@clerk/backend' ) ;
1120 return {
1221 ...actual ,
13- createClerkClient : ( ) => {
14- return {
15- authenticateRequest : ( ...args : any ) => authenticateRequestMock ( ...args ) ,
16- } ;
17- } ,
22+ createClerkClient : ( ...args : any [ ] ) => createClerkClientMock ( ...args ) ,
1823 } ;
1924} ) ;
2025
@@ -24,6 +29,38 @@ describe('withClerkMiddleware(options)', () => {
2429 vi . restoreAllMocks ( ) ;
2530 } ) ;
2631
32+ test ( 'creates the request client with plugin runtime keys' , async ( ) => {
33+ authenticateRequestMock . mockResolvedValueOnce ( {
34+ headers : new Headers ( ) ,
35+ toAuth : ( ) => ( {
36+ tokenType : 'session_token' ,
37+ } ) ,
38+ } ) ;
39+ const fastify = Fastify ( ) ;
40+ await fastify . register ( clerkPlugin , {
41+ secretKey : 'runtime_secret_key' ,
42+ publishableKey : 'runtime_publishable_key' ,
43+ } ) ;
44+
45+ fastify . get ( '/' , ( request : FastifyRequest , reply : FastifyReply ) => {
46+ const auth = getAuth ( request ) ;
47+ reply . send ( { auth } ) ;
48+ } ) ;
49+
50+ const response = await fastify . inject ( {
51+ method : 'GET' ,
52+ path : '/' ,
53+ } ) ;
54+
55+ expect ( response . statusCode ) . toEqual ( 200 ) ;
56+ expect ( createClerkClientMock ) . toHaveBeenLastCalledWith (
57+ expect . objectContaining ( {
58+ secretKey : 'runtime_secret_key' ,
59+ publishableKey : 'runtime_publishable_key' ,
60+ } ) ,
61+ ) ;
62+ } ) ;
63+
2764 test ( 'handles signin with Authorization Bearer' , async ( ) => {
2865 authenticateRequestMock . mockResolvedValueOnce ( {
2966 headers : new Headers ( ) ,
0 commit comments