Skip to content

Commit 41a2ecb

Browse files
committed
🚨 Make redirect mock mirror SvelteKit’s Redirect shape (#2446)
1 parent cb72c1c commit 41a2ecb

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/test/lib/utils/auth_forms.test.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import { vi, expect, describe, beforeEach, afterEach, test } from 'vitest';
22

33
// Mock external dependencies BEFORE importing the module under test
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

1018
vi.mock('sveltekit-superforms/server', () => ({
1119
superValidate: vi.fn(),
@@ -119,7 +127,10 @@ describe('auth_forms', () => {
119127
test('expect to redirect to home page if user is already logged in', async () => {
120128
const mockLocals = createMockLocals(true);
121129

122-
await expect(initializeAuthForm(mockLocals)).rejects.toThrow('Redirect: 303 /');
130+
await expect(initializeAuthForm(mockLocals)).rejects.toMatchObject({
131+
name: 'Redirect',
132+
location: '/',
133+
});
123134
expect(redirect).toHaveBeenCalledWith(303, '/');
124135
});
125136

0 commit comments

Comments
 (0)