1
1
// There is no need to execute the complete authenticateRequest to test clerkMiddleware
2
2
// This mock SHOULD exist before the import of authenticateRequest
3
3
import { AuthStatus , constants } from '@clerk/backend/internal' ;
4
- import { describe , expect } from '@jest/globals' ;
5
4
// used to assert the mock
6
5
import assert from 'assert' ;
7
6
import type { NextFetchEvent } from 'next/server' ;
8
7
import { NextRequest , NextResponse } from 'next/server' ;
8
+ import { afterAll , beforeAll , beforeEach , describe , expect , it , vi } from 'vitest' ;
9
9
10
10
import { clerkClient } from '../clerkClient' ;
11
11
import { clerkMiddleware } from '../clerkMiddleware' ;
12
12
import { createRouteMatcher } from '../routeMatcher' ;
13
13
import { decryptClerkRequestData } from '../utils' ;
14
14
15
15
const publishableKey = 'pk_test_Y2xlcmsuaW5jbHVkZWQua2F0eWRpZC05Mi5sY2wuZGV2JA' ;
16
- const authenticateRequestMock = jest . fn ( ) . mockResolvedValue ( {
16
+ const authenticateRequestMock = vi . fn ( ) . mockResolvedValue ( {
17
17
toAuth : ( ) => ( {
18
18
debug : ( d : any ) => d ,
19
19
} ) ,
20
20
headers : new Headers ( ) ,
21
21
publishableKey,
22
22
} ) ;
23
23
24
- jest . mock ( '../clerkClient' , ( ) => {
24
+ vi . mock ( '../clerkClient' , ( ) => {
25
25
return {
26
26
clerkClient : ( ) => ( {
27
27
authenticateRequest : authenticateRequestMock ,
28
- telemetry : { record : jest . fn ( ) } ,
28
+ telemetry : { record : vi . fn ( ) } ,
29
29
} ) ,
30
30
} ;
31
31
} ) ;
@@ -37,8 +37,8 @@ const consoleWarn = console.warn;
37
37
const consoleLog = console . log ;
38
38
39
39
beforeAll ( ( ) => {
40
- global . console . warn = jest . fn ( ) ;
41
- global . console . log = jest . fn ( ) ;
40
+ global . console . warn = vi . fn ( ) ;
41
+ global . console . log = vi . fn ( ) ;
42
42
} ) ;
43
43
afterAll ( ( ) => {
44
44
global . console . warn = consoleWarn ;
@@ -47,11 +47,13 @@ afterAll(() => {
47
47
48
48
// Removing this mock will cause the clerkMiddleware tests to fail due to missing publishable key
49
49
// This mock SHOULD exist before the imports
50
- jest . mock ( '../constants' , ( ) => {
50
+ vi . mock ( import ( '../constants.js' ) , async importOriginal => {
51
+ const actual = await importOriginal ( ) ;
51
52
return {
53
+ ...actual ,
54
+ ENCRYPTION_KEY : 'encryption-key' ,
52
55
PUBLISHABLE_KEY : 'pk_test_Y2xlcmsuaW5jbHVkZWQua2F0eWRpZC05Mi5sY2wuZGV2JA' ,
53
56
SECRET_KEY : 'sk_test_xxxxxxxxxxxxxxxxxx' ,
54
- ENCRYPTION_KEY : 'encryption-key' ,
55
57
} ;
56
58
} ) ;
57
59
@@ -74,7 +76,7 @@ const mockRequest = (params: MockRequestParams) => {
74
76
describe ( 'ClerkMiddleware type tests' , ( ) => {
75
77
// create a copy to test the types only
76
78
// running this function does nothing, it is used purely for type checking
77
- const clerkMiddlewareMock = jest . fn ( ) as typeof clerkMiddleware ;
79
+ const clerkMiddlewareMock = vi . fn ( ) as typeof clerkMiddleware ;
78
80
it ( 'can receive the appropriate keys' , ( ) => {
79
81
clerkMiddlewareMock ( { publishableKey : '' , secretKey : '' } ) ;
80
82
clerkMiddlewareMock ( { secretKey : '' } ) ;
@@ -589,7 +591,7 @@ describe('clerkMiddleware(params)', () => {
589
591
590
592
describe ( 'debug' , ( ) => {
591
593
beforeEach ( ( ) => {
592
- ( global . console . log as jest . Mock ) . mockClear ( ) ;
594
+ global . console . log . mockClear ( ) ;
593
595
} ) ;
594
596
595
597
it ( 'outputs debug logs when used with only params' , async ( ) => {
0 commit comments