Skip to content

Commit a0fd8fc

Browse files
committed
chore: fix nextjs tests
1 parent 25fa70a commit a0fd8fc

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

packages/nextjs/src/__tests__/webhooks.test.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@ const mockSuccessResponse = {
1010
object: 'event',
1111
} as any;
1212

13-
const mockError = new Error('Missing required Svix headers: svix-id, svix-timestamp, svix-signature');
14-
1513
vi.mock('@clerk/backend/webhooks', () => ({
16-
verifyWebhook: vi.fn().mockImplementation((request: Request) => {
17-
const svixId = request.headers.get('svix-id');
18-
const svixTimestamp = request.headers.get('svix-timestamp');
19-
const svixSignature = request.headers.get('svix-signature');
14+
verifyWebhook: vi.fn().mockImplementation(async (request: any) => {
15+
// Support both Fetch API Request and plain object
16+
const getHeader = (key: string) => {
17+
if (request instanceof Request) {
18+
return request.headers.get(key);
19+
}
20+
return request.headers?.[key];
21+
};
22+
23+
const svixId = getHeader('svix-id');
24+
const svixTimestamp = getHeader('svix-timestamp');
25+
const svixSignature = getHeader('svix-signature');
2026

2127
if (!svixId || !svixTimestamp || !svixSignature) {
22-
throw mockError;
28+
throw new Error('Missing required Svix headers: svix-id, svix-timestamp, svix-signature');
2329
}
2430

2531
return mockSuccessResponse;
@@ -88,7 +94,9 @@ describe('verifyWebhook', () => {
8894
aborted: false,
8995
} as unknown as NextApiRequest;
9096

91-
await expect(verifyWebhook(mockNextApiRequest)).rejects.toThrow(mockError);
97+
await expect(verifyWebhook(mockNextApiRequest)).rejects.toThrow(
98+
'Missing required Svix headers: svix-id, svix-timestamp, svix-signature',
99+
);
92100
});
93101
});
94102
});

0 commit comments

Comments
 (0)