Skip to content

Commit 0e5c172

Browse files
committed
🚨 Make redirect mock mirror SvelteKit’s Redirect shape (#2446)
1 parent e86c83a commit 0e5c172

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/test/lib/utils/authorship.test.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import { 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

1018
import {
1119
ensureSessionOrRedirect,
@@ -49,7 +57,10 @@ describe('ensureSessionOrRedirect', () => {
4957
},
5058
} as unknown as App.Locals;
5159

52-
await expect(ensureSessionOrRedirect(mockLocals)).rejects.toThrow(/Redirect \d{3} \/login/);
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(/Redirect \d{3} \/login/);
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(/Redirect \d{3} \/login/);
102+
await expect(getLoggedInUser(mockLocals)).rejects.toMatchObject({
103+
name: 'Redirect',
104+
location: '/login',
105+
});
89106
});
90107
});
91108

0 commit comments

Comments
 (0)