11import { expect , test , describe , vi } from 'vitest' ;
22
33// Mock modules
4- vi . mock ( '@sveltejs/kit' , ( ) => ( {
5- redirect : vi . fn ( ) . mockImplementation ( ( status : number , location : string ) => {
6- throw new Error ( `Redirect ${ status } ${ location } ` ) ;
7- } ) ,
8- } ) ) ;
4+ vi . mock ( '@sveltejs/kit' , ( ) => {
5+ const redirectImpl = ( status : number , location : string ) => {
6+ const error = new Error ( 'Redirect' ) ;
7+
8+ ( error as any ) . name = 'Redirect' ;
9+ ( error as any ) . status = status ;
10+ ( error as any ) . location = location ;
11+
12+ throw error ;
13+ } ;
14+
15+ return { redirect : vi . fn ( redirectImpl ) } ;
16+ } ) ;
917
1018import {
1119 ensureSessionOrRedirect ,
@@ -49,7 +57,10 @@ describe('ensureSessionOrRedirect', () => {
4957 } ,
5058 } as unknown as App . Locals ;
5159
52- await expect ( ensureSessionOrRedirect ( mockLocals ) ) . rejects . toThrow ( / R e d i r e c t \d { 3 } \/ l o g i n / ) ;
60+ await expect ( ensureSessionOrRedirect ( mockLocals ) ) . rejects . toMatchObject ( {
61+ name : 'Redirect' ,
62+ location : '/login' ,
63+ } ) ;
5364 } ) ;
5465} ) ;
5566
@@ -74,7 +85,10 @@ describe('getLoggedInUser', () => {
7485 } ,
7586 } as unknown as App . Locals ;
7687
77- await expect ( getLoggedInUser ( mockLocals ) ) . rejects . toThrow ( / R e d i r e c t \d { 3 } \/ l o g i n / ) ;
88+ await expect ( getLoggedInUser ( mockLocals ) ) . rejects . toMatchObject ( {
89+ name : 'Redirect' ,
90+ location : '/login' ,
91+ } ) ;
7892 } ) ;
7993
8094 test ( 'expect to redirect when session exists but no user' , async ( ) => {
@@ -85,7 +99,10 @@ describe('getLoggedInUser', () => {
8599 user : null ,
86100 } as unknown as App . Locals ;
87101
88- await expect ( getLoggedInUser ( mockLocals ) ) . rejects . toThrow ( / R e d i r e c t \d { 3 } \/ l o g i n / ) ;
102+ await expect ( getLoggedInUser ( mockLocals ) ) . rejects . toMatchObject ( {
103+ name : 'Redirect' ,
104+ location : '/login' ,
105+ } ) ;
89106 } ) ;
90107} ) ;
91108
0 commit comments